- Home /
Change rigidbody direction
Hi, i want to move a rigidbody from a start position to the final. I have a final position A and a final position b. If i say to the rigidbody to move to A, it moves to A. If i say to move to B , it moves to B. But if i say to move to A and then to move to B, it rotates to B but it moves to the opposite direction of B. The same happend if i say :move to B and then to A, it rotates to A but
it moves to the opposite direction of A.I try to split the direction vector in to two different var Vector or use one var Vector, but nothing. Thanks for the help. This are the lines
private Vector3 dir;
private GameObject A;
private GameObject B;
dir = (new Vector3 (A.transform.position.x,transform.position.y,A.transform.position.z)-transform.position).normalized;
transform.rigidbody.AddForce(dir*Time.deltaTime*350,ForceMode.Acceleration);
transform.localRotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dir), 500.0f * Time.deltaTime);
if(A.transform.parent==transform)
{
dir = (new Vector3 (B.transform.position.x,transform.position.y,B.transform.position.z)-transform.position).normalized;
transform.rigidbody.AddForce(new Vector3(-dir.x,0,-dir.z)*Time.deltaTime*350,ForceMode.Acceleration);
transform.localRotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dir, 500.0f * Time.deltaTime);
}
EDIT: I have found that it goes correctly to the determined position only if the targetPosition Z Value is higher than the rigidbody. If the targetPosition Z Value is lower than the rigidbody,the body goes to the opposite direction.Why?
Answer by Ame · Jul 29, 2012 at 05:01 PM
I've found the problem. It was the box collider. If the box collider size Z value is different than 0 , it goes to another direction. So the Z value of the box collider must be 0. I don't know why. I didn't know that the Z value of the collider could influence the movement.
EDIT: to put a collider , add an empty gameObject as box collider inside the prefab/object. Don't add box the collider to all the prefab.Add as emptyObject and you can resize the box collider Z Value.