- Home /
Question by
siddharth3322 · Nov 02, 2014 at 03:33 PM ·
transformrigidbody2dvelocity
Smoothly move continuous moving object
Currently I am working on clone of Ping Pong type game. I want to move my ball continuously on screen. At present I have code for ball movement but it looks jittery.
public class ContinuousMoveSinglePlayer : MonoBehaviour
{
private bool isStartMoving;
private Vector3 direction;
private float speed = 10f;
private int opponentPaddleCounter;
// Use this for initialization
void Start ()
{
InitializeValues ();
}
private void InitializeValues ()
{
float xDirection = Random.Range (0, 2) * 2 - 1;
float yDirection = Random.Range (0, 2) * 2 - 1;
direction = new Vector3 (xDirection, yDirection).normalized;
}
// Update is called once per frame
void FixedUpdate ()
{
if (isStartMoving) {
rigidbody2D.velocity = direction * speed;
}
}
void OnCollisionEnter2D (Collision2D otherCollision)
{
if (otherCollision.gameObject.CompareTag (Constants.TAG_OPPONENT_PADDLE)) {
opponentPaddleCounter++;
if (opponentPaddleCounter > Constants.BALL_SPEED_LIMIT) {
opponentPaddleCounter = 0;
speed += 1f;
}
}
Vector3 normal = otherCollision.contacts [0].normal;
direction = Vector3.Reflect (direction, normal);
}
}
I want to move ball smoothly on screen. I already tried with following code
transform.position = Vector3.Lerp(transform.position,(transform.position + direction), Time.deltaTime * speed);
But above line creates poor collision response. It detect collision with some time delay so that it looks likes ball go inside the wall. I need your suggestions to solve this problem.
Comment
Answer by KayelGee · Nov 03, 2014 at 12:13 PM
Multiplying by deltaTime might help.
rigidbody2D.velocity = direction * speed * Time.deltaTime;
I have already written code in FixedUpdate method so as per my concept Time.deltaTime does not change any thing here. Also in running example it shows clearly some jerk in movement.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170540im_/https://answers.unity.com/themes/thub/images/avi.jpg)