- Home /
The ball is stuck in the beginning position
Hello, Im making a simple endless run with a ball who moves in "rails" with finger swipes. My problem is that after i made the code for swipes the ball is stuck in the beginning position.
public float playerSpeed;
public float diagonalSpeed;
public Vector3 destination;
void Start()
{
destination = transform.position;
}
void Update()
{
GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, destination, diagonalSpeed *
Time.deltaTime);
if (SwipeManager.IsSwipingLeft())
{
destination = new Vector3(transform.position.x - 4f,transform.position.y, transform.position.z);
}
if (SwipeManager.IsSwipingRight())
{
destination = new Vector3(transform.position.x + 4f, transform.position.y,
transform.position.z);
}
}
I think its because i call the vector3.Lerp every frame but i dont know how to fix this. Thank you very much for the help.
Answer by unity_ek98vnTRplGj8Q · Feb 20, 2020 at 10:42 PM
You are never updating destination to move forward with the player, it is always staying at the start line. That being said, you are going to have weird behavior if you are trying to move your object with BOTH a rigidbody and by setting its position. If you want to use a non-kinematic rigidbody, you should add forces to the left or right instead of just moving the object directly. You will likely rather want to use a kinematic rigidbody though, but you will have to use Rigidbody.MovePosition and will have to add the forward velocity and gravity to your movement vector every frame. Additionally, you should move your code to FixedUpdate instead of Update, and change your Time.deltaTime's to Time.fixedDeltaTime. Let me know if you have any questions on how to proceed if you are not sure.
Your answer
Follow this Question
Related Questions
Simple 2D smooth ball movement 1 Answer
How to create icy floor? c# 1 Answer
Ball Rolling System 1 Answer
Player-controlled rigidbody ball sometimes slowing down for no reason 0 Answers