- Home /
Rotate/Roll
I need my ship to roll left when I hit the left arrow key and roll right when I hit the right arrow key. How should I do that?
(I've searched Unity Answers and all the scripts didn't work. I tried Unity Forums, no help there. Even Google didn't help me, so unless you're absolutely sure it will work, don't direct me to another page.)
Sooner or later you will have to write your own scripts. I give you the advice: learn scripting! you may still want to copy and paste scripts, but with some knowledge you can better understand why a script doesn't work. And Google can help you, trust me ;)
Is rotation along one axis all you want? http://unity3d.com/support/documentation/ScriptReference/Transform-localEulerAngles.html I may write a simple script for you if you give some more details. But i'd like to say: If you want to learn scripting, this is one of the easier things to start out with.
Answer by AngryMarine · Aug 29, 2011 at 02:39 AM
I think this might help you out. Keep in mind this type of movement will not allow colliders to work well. To use physics and colliders and such you'll need to use rigidbody.AddRelativeForce & rigidbody.AddRelativeTorque.
var rightSpeed = 0.05; var leftSpeed = -0.05; /// Same as rightSpeed but a negative
function Update () {
if(Input.GetKey("right"))
{
transform.Rotate(Vector3.forward, rightSpeed);
}
if(Input.GetKey("left"))
{
transform.Rotate(Vector3.forward, leftSpeed);
}
}
If i remember right you use the axis with which to rotate around. The Z axis is forward so you want to rotate around IT which results in the type of banking/rolling movement you are seeking. Just make sure this script is attached to the GameObject you want to rotate. NOT a parent or child. It must be directly attached to what you want to spin. So if it's an airplane, this must be attached to the airplane gameobject which has the mesh filter as a component. I sometimes use an empty GameObject to contain a 'child' GO which I instantiate. In these cases, the scripts should be attached to the object which you want manipulated and not the empty GameObject holder.
It works, and I was thinking of trying this. The problem is, the camera can't turn with it. I need the camera to spin as the ship spins.
Never$$anonymous$$d. The rotate was on the ship, and the camera was following the parent. It works now that I changed the target. Thanks.
Answer by roamcel · Aug 09, 2011 at 11:42 AM
transform.RotateAroundLocal(transform.forward,5);
where 5 is the amount of degrees to rotate along the transform.forward axis
For a more advanced solution, access the unity reference for transform
quite clearly you need to change the transform.forward to transform.right or transform.up based on your gameobject's orientation, and of course you need to be sure that it's origin is in the center of the object. If it isn't, your model is flawed and should be edited and reimported.
So should I add this to my move script, or make a script of it's own?
entirely up to you, but you probably just save time by adding it to your existing scripts. win-win tip: access the official demo projects and copy from them for these basic problems.
Answer by DayyanSisson · Aug 10, 2011 at 07:36 AM
Ok I modified my moving script.
var speed = 50.0;
function Update ()
{
if(Input.GetKey("right"))
{
transform.RotateAround(transform.forward,0.01);
}
if(Input.GetKey("left"))
{
transform.RotateAround(transform.forward,-0.01);
}
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(0, 0, z);
}
The problem here is the line:
transform.RotateAround(transform.forward,0.01);
If I change the `transform.forward` into a `transform.up`, the ship rotates around the y axis, but I need it to rotate on the z axis, and `transform.forward` isn't giving me that. How should I fix it?
Answer by roamcel · Aug 28, 2011 at 05:02 AM
Now that you've attached it to the parent gameobject, it should be rotating around
transform.forward
Like I said, I think that it's treating my transform's as vector3's. Well only for this script. It doesn't work, right, forward, whatever. It still doesn't work. I'll try one thing and get back to you.
Thank you. Although he actually achieved the answer, you helped a lot. Thanks for your time and patience.
Your answer
Follow this Question
Related Questions
How to roll the ball? 1 Answer
How to make a ball that can roll? 2 Answers
Detect the up axis when rolling a cube? 2 Answers
Problem with a ball to roll 0 Answers
Ball Z-Axis Rolling 1 Answer