constant moving up?

Hi,

I'm trying to get an object to move up, always with the same speed. I first used a simple

var speed = X;

transform.position.y += speed*Time.deltaTime;
transform.position.z = -0.5; // So that the object doesnt move in depth since it is a 2d game.

The Problem with this is, that if the object hits an obstacle sometimes, not always, the speed reduces itself. If I hit an object again, the speed is constant again.

I froze rotation with the rigidbody, so a rotation towards the camera is not possible.

What could be the problem?

2 Answers

2

The issue is that the rigidbody also changes position of the object, in opposite direction your movement. When you use a rigidbody, you better do the movement on the rigidbody, not on the transform, for better physics behaviour.

quick solution:

var position : Vector3;
var speed : float = X;

function Start() {
    position = rigidbody.position;
}

function FixedUpdate() {
    position.y += speed * Time.fixedDeltaTime;
    rigidbody.MovePosition(position);
}

Just for the people, who maybe got a similiar problem:

The problem was the velocity of the rigidbody.

function Update(){
   rigidbody.velocity = Vector3(0,0,0);
}

This solved the issue for me.