in progress bug movement
This commit is contained in:
@@ -4,22 +4,56 @@ using UnityEngine;
|
||||
|
||||
public class BugMovement : MonoBehaviour
|
||||
{
|
||||
public LayerMask layerMask;
|
||||
public float speed;
|
||||
private bool movingRight = true;
|
||||
Rigidbody2D bugBody;
|
||||
private Transform groundDetection;
|
||||
[SerializeField] private int secondsGoingLeft = 1;
|
||||
[SerializeField] private int secondsGoingRight = 1;
|
||||
[SerializeField] private float movementSpeed = 10;
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private int leftAccumalator = 0;
|
||||
private int rightAccumalator = 0;
|
||||
private Rigidbody2D rigidBody;
|
||||
|
||||
private int framesPerSecond => 60;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
transform.Translate(Vector2.right * speed * Time.deltaTime);
|
||||
// Physics2D.IgnoreLayerCollision(LayerMask.GetMask())
|
||||
|
||||
rigidBody = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
var totalFramesGoingLeft = framesPerSecond * secondsGoingLeft;
|
||||
var totalFramesGoingRight = framesPerSecond * secondsGoingRight;
|
||||
|
||||
if (leftAccumalator <= totalFramesGoingLeft)
|
||||
{
|
||||
MoveLeft();
|
||||
leftAccumalator++;
|
||||
|
||||
if (leftAccumalator == totalFramesGoingLeft)
|
||||
rightAccumalator = 0;
|
||||
}
|
||||
else if (rightAccumalator <= totalFramesGoingRight)
|
||||
{
|
||||
MoveRight();
|
||||
rightAccumalator++;
|
||||
|
||||
if (rightAccumalator == totalFramesGoingRight)
|
||||
leftAccumalator = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveLeft()
|
||||
{
|
||||
var actualSpeed = movementSpeed * -1;
|
||||
rigidBody.velocity += new Vector2(actualSpeed, 0);
|
||||
}
|
||||
|
||||
private void MoveRight()
|
||||
{
|
||||
var actualSpeed = movementSpeed;
|
||||
rigidBody.velocity += new Vector2(actualSpeed, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user