How can I make a object follow another object by rigidbody

if(Quaternion.Angle(reference.rotation,transform.rotation) > 0){
Vector3 axis = Vector3.Cross(transform.forward,reference.forward);
Quaternion delta = Quaternion.AngleAxis(Quaternion.Angle(transform.rotation,reference.rotation),axis.normalized);
rigidbody.angularVelocity = Vector3.zero;
if(axis.y < 0){
delta.eulerAngles.Scale(new Vector3(0,-1,0));
rigidbody.AddTorque(delta.eulerAngles * Mathf.PI * 6/180,ForceMode.VelocityChange);
}else{
rigidbody.AddTorque(delta.eulerAngles * Mathf.PI * 6/180,ForceMode.VelocityChange);
}
}else{
rigidbody.angularVelocity = Vector3.zero;
}
}
This is the code I write in FixedUpdate. Reference is the Object I want to follow. When I rotate clockwise, it works. But when I rotate conter-clockwise, it wrong. It rotate in the wrong direction and wrong speed. How can I do?

Looking at your code, here are a few things to consider:

  • Doing what you are doing, the chance of getting no angle between the reference and the object is very small.
  • Unity creates euler angles from the Quaternion. There are multiple euler angle representations for any given physical representation, and the one Unity reports may not be what you expect.
  • AddTorque() is like AddForce(). Adding new torque does not cancel out old torque unless your drag is set very high.
  • You can assign directly to ‘Rigidbody.angularVelocity’. The components will be in Radians.
  • You might take a look at Rigidbody.MoveRotation(). This is an untested suggestion.

Use following scripts:

	public GameObject obj;
	public float speed;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		rigidbody.AddForce(obj.transform.forward*-speed);
	}
}