Hi everyone .
i want to add some force to transform.forward of a rigid body sphere in update() but when the game is run , sphere and forward is rotated with and force add to the wrong way ?
how can i fix it ?[/img]
var line:Vector3;
function Update () {
line.x=gameObject.Find("tar").transform.position.x-transform.position.x;
line.y=gameObject.Find("tar").transform.position.y-transform.position.y;
line.z=gameObject.Find("tar").transform.position.z-transform.position.z;
transform.rigidbody.AddForce(line * 5);
}
I didn’t fully understand your question, but I think you want to apply the force in the ‘fixed update’ function, not in the ‘update’ function.
Also, assuming js has support for operator overloading, you can probably replace these lines:
line.x=gameObject.Find("tar").transform.position.x-transform.position.x;
line.y=gameObject.Find("tar").transform.position.y-transform.position.y;
line.z=gameObject.Find("tar").transform.position.z-transform.position.z;
With this:
line = gameObject.Find("tar").transform.position - transform.position;
I’d also cache that Find call in a Start function so you are running it every update. ![]()