I'm certain I'm probably doing this wrong, so I'm asking here to find out the right way.
I am trying to rotate a rectangular object's localRotation to a euler angle on the Z axis. I am currently using a kinematic rigidbody and the following code to 1) Calculate the new Z rotation by adding some euler angles to the local rotation, and 2) Using a Quaternion.Lerp to have the rotation occur over a specific interval.
Here is the code I'm using:
1) Calculate the new Euler Angle by taking the local rotation and adding a Z eulerAngle:
Vector3 Destination;
Quaternion StartRotation = this.transform.localRotation;
float myRotation = 90.0f;
Destination = new Vector3(this.transform.localRotation.eulerAngles.x, this.transform.localRotation.eulerAngles.y, this.transform.localRotation.eulerAngles.z + myRotation);
2) Start the coroutine which takes care of the rotation:
yield return StartCoroutine(RotateLerp(StartRotation.eulerAngles, Destination));
3) And here's the co-routine that rotates over time:
IEnumerator RotateLerp(Vector3 StartPosEuler, Vector3 EndPosEuler)
{
float i = 0.0f;
float rate = 1.0f/(RotateSpeed);
while (i < 1.0) {
i += Time.deltaTime * rate;
this.transform.localRotation = Quaternion.Lerp(Quaternion.Euler(StartPosEuler), Quaternion.Euler(EndPosEuler), i);
yield return true;
}
}
All of this works great. The object rotates fine over time. My problem is that I have a another non-kinematic rigidbody sitting on top of the object, and I was hoping that when I increase the speed of the rotation, this "target" rigidbody would get "tossed" into the air or out of the way when the rotating object collides with it. However, it seems to just slightly "push" it enough to get by. It doesn't really toss it at all, which I suppose is to be expected since there is no real force involved here. I realize this is normal behavior, but I'm new at this and not really sure of a better way.
I've dug around a bit on here but I can't find a particularly clear answer. I suppose what I need to know is how to apply a force to the rigidbody such that the rigidbody would turn in the Z-axis to a particular euler angle over time. Any advice on this issue would be greatly appreciated.
Thank you so much!!