Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DayyanSisson · Aug 09, 2011 at 04:41 AM · axisrollrollingarrow-keys

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.)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sven1994 · Aug 09, 2011 at 10:16 AM 0
Share

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 ;)

avatar image Sir Keyzinburga · Aug 09, 2011 at 10:48 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayyanSisson · Aug 29, 2011 at 11:55 PM 0
Share

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.

avatar image DayyanSisson · Aug 30, 2011 at 12:08 AM 0
Share

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.

avatar image AngryMarine · Aug 30, 2011 at 07:00 PM 0
Share

No problem, glad I could help.

avatar image
0

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

Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayyanSisson · Aug 10, 2011 at 07:08 AM 0
Share

I tried that but my ship just moves up and down.

avatar image roamcel · Aug 10, 2011 at 07:19 AM 0
Share

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.

avatar image DayyanSisson · Aug 10, 2011 at 07:21 AM 0
Share

So should I add this to my move script, or make a script of it's own?

avatar image roamcel · Aug 10, 2011 at 07:30 AM 0
Share

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.

avatar image DayyanSisson · Aug 10, 2011 at 07:36 AM 0
Share

alright, look at my modified script

Show more comments
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roamcel · Aug 10, 2011 at 07:51 AM 0
Share

transform.right?

avatar image DayyanSisson · Aug 10, 2011 at 11:22 PM 0
Share

Yes. I did transform right.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayyanSisson · Aug 29, 2011 at 12:42 AM 0
Share

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.

avatar image DayyanSisson · Aug 30, 2011 at 12:08 AM 0
Share

Thank you. Although he actually achieved the answer, you helped a lot. Thanks for your time and patience.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges