- Home /
Rotate rigidbody over time
Hello Everyone,
I am trying to rotate a rigidbody smoothly over time, the setting is: there is a bridge connecting A and B (pivot point at A), I need to rotate it so that it connects A and C.
A ------- B
|
|
C
Please note that my character needs to stand on the bridge during rotation, therefore I can't use any of the following:
1) transform.rotation = Quaternion.Slerp (...) ,since the character would fall
2) rigidbody.rotation = Quaternion.Slerp (...) ,same as above
3) rigidbody.MoveRotation(...) since it doesn't show the movement process
I also did try AddTorque but it seems to rotate around objects center, not pivot point.
Any pointers would be appreciated :)
perhaps you can change the pivot point or just come up with a solution that would prevent the player from falling off the bridge during rotation?
thanks, but changing the pivot point wouldn't help cause I need one bridge rotating back and force to connect these 3 points (A, B,C).
and I need character not to fall as well as move along with the bridge.
Try using localEulerAngles. In this example, I set a max angle on the Y axis to 90, and a speed of 15 * Time.deltaTime.
//C-Sharp Example
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
public float max = 90f;
public float speed = 15f;
Vector3 cur;
void Start()
{
cur = transform.localEulerAngles;
max = transform.localEulerAngles.y + max;
}
void Update()
{
transform.localEulerAngles = cur;
if(cur.y < max)
{
cur.y += speed * Time.deltaTime;
}
}
}
//Javascript Example
var max : float = 90f;
var speed : float = 15f;
private var cur : Vector3;
function Start()
{
cur = transform.localEulerAngles;
max = transform.localEulerAngles.y + max;
}
function Update()
{
transform.localEulerAngles = cur;
if(cur.y < max)
{
cur.y += speed * Time.deltaTime;
}
}
Answer by chronicfail · Aug 12, 2013 at 04:20 PM
You can change the pivot point- use
var center : Vector3;
function Start () {
rigidbody.centerOfMass=center;
}
but I think the standard solution to this is to make an animation for the bridge movement.
Answer by Owen-Reynolds · Aug 13, 2013 at 12:12 AM
If you want to do it using physics, "anchor" the end of the bridge. A cheap way is to set centerOfMass as chroncil mentions (a CoM of 000 is the real object pivot point. It uses local coords, so set X to negative 1/2 length of bridge.) The bridge will act like it has a lead weight at that end, and the rest is paper. Could also use a hinge (can attach/remove these on the fly if you need to.)
Then can push it with rigidbody.MoveTowards
(just don't move it all at once - a little each frame like in the docs example.) Or can set AngularVelocity directly (in radians -- 6.28 is one full spin/second.)
The problem, is, the player will tend to slide off, even if you increase friction. This is probably a "gamey" problem. A real bridge rotating at a speed that looks good in a game really would fling human beings against and over the rails, and off the end.
The most common solution is to cheat by temporarily childing the player to the moving bridge. Then you can just use transform.rotate (if the pivot is moved to one end through parent tricks) or whatever is easier.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to rotate an object to a specific angle with angularVelocity? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Bullet not moving towards player 3 Answers