• 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 Averlion · Jun 21, 2010 at 07:52 PM · rotateshiprolling

rotate my ship or rolling it to left an right

Hi i want that my ship rolles when i press "q" to left and on "e" to the right. Every rotation i tried didnt work. Here is an example of what i tried.

var speed = 200; var acc = 60; var acc2 = 160; var mom = 0; var momb = 0; var movleft = 0; var movright = 0; var rotleft = 0; var rotright = 0; var besch = 1; var beschr = 20; var hyper = 2000;

var smooth = 2.0; var tiltAngle = 30.0;

var rotateSpeed = 20.0;

function Update () {

if (mom < 0){ mom = 0; } if (Input.GetKey ("up") || Input.GetKey ("w")) { if (mom <= speed){ mom = mom + besch; if (mom > speed){ mom = speed; } } } else { if (mom >= 0){ mom = mom - besch; } else { mom = 0; } } if (Input.GetKey ("down") || Input.GetKey ("s")) {

     mom = mom - besch;
     if (mom &lt; 0){
         mom = 0;
     }

} if (Input.GetKey ("left") || Input.GetKey ("a")) { if (movleft <= acc){ movleft = movleft + besch; if (movleft >= acc){ movleft = acc; } } } else { if (movleft > 0){ movleft = movleft - besch; } } if (Input.GetKey ("right") || Input.GetKey ("d")) { if (movright <= acc){ movright = movright + besch; if (movright >= acc){ movright = acc; } } } else { if (movright > 0){ movright = movright - besch; } }

if (Input.GetKey ("e")) { if (rotright <= acc2){ rotright = rotright + beschr; if (rotright >= acc2){ rotright = acc2; } } } else { if (rotright > 0){ rotright = rotright - beschr; } }

if (Input.GetKey ("q")) { if (rotleft <= acc2){ rotleft = rotleft + beschr;

 }

} else { if (rotleft > 0){ rotleft = rotleft - beschr; } }

if (Input.GetKey ("j")) {

     speed = hyper;
     besch = 10;

} if (mom < 0){ mom = 0; } transform.Translate(0, 0, Time.deltaTime*mom); transform.Translate(0, 0, -Time.deltaTime*momb); transform.Translate(-Time.deltaTime*movleft, 0,0); transform.Translate(Time.deltaTime*movright, 0, 0);

//transform.Rotate (0, 0, -Time.deltaTime*rotright); //transform.Rotate (0, 0, Time.deltaTime*rotleft); //transform.Rotate(Vector3(0, 0, 1), Time.deltaTime*rotleft); //transform.Rotate(Vector3(0, 0, 1), -Time.deltaTime*rotright);

//var tiltAroundZ = Input.GetAxis("Horizontal") tiltAngle; var tiltAroundZ = Input.GetKey ("q") 30; //var target = Quaternion.Euler (0, tiltAroundX, 0); transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);

}

Comment
Add comment · Show 1
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 Pirate___man · Jun 22, 2010 at 12:19 AM 0
Share

Are you trying to do a complete barrel roll? Or just a tilt?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pirate___man · Jun 21, 2010 at 08:00 PM

You could try animating it in your modeling application... That would be the easiest thing to do. Just animate the ship rolling and then tell unity to play the animation when you press the button and while playing the animation move the ship over to the side. Hope this helps!! Have fun making your ship game!

Comment
Add comment · 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
0

Answer by Tetrad · Jun 21, 2010 at 08:01 PM

Why are you multiplying by negative delta time? What are the values of rotleft/rotright and tiltAngle? Why does try3 not use tiltAroundZ anywhere? (Actually that'll probably solve your problem, add tiltAroundZ as the z value on your target rotation.)

Have you considered using something more off the shelf, like perhaps this script: http://www.unifycommunity.com/wiki/index.php?title=ShipControls

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 Pirate___man · Jun 21, 2010 at 08:08 PM 0
Share

Actually I found that for my ship game those default ship controls failed miserably. I wouldn't recommend them!

avatar image Averlion · Jun 21, 2010 at 08:12 PM 0
Share

i added in my question the complete script

avatar image
0

Answer by Novodantis 1 · Jun 21, 2010 at 11:33 PM

I profess that the script doesn't make a lot of sense to me. When you say it didn't work, was that not at all, or different to expected? I can see some bits of code here that will give rotation behaviour, but can't quite follow it. For instance the Quaternion.Slerp() seems to call on "target" but I don't see that declared anywhere.

Most of the problem with rotations of this kind are down to rotating the wrong axis, or by a value that is too large or small. A ship should probably roll on its z axis (in 3D). If it is rotated by a value you are adding/subtracting to each frame based on key input, remember to decay it (movright *= 0.9; each frame for example) or you'll just keep spinning.

For roll, I would use one value like so (heavily simplified):

var rollSpeed = 5.0; var rollAmount = 0.0;

function Update() {

rollAmount *= 0.9;

if (Input.GetKey("q")) { rollAmount += rollSpeed ; } else if (Input.GetKey("e")) { rollAmount -= rollSpeed ; }

transform.Rotate(0, 0, rollAmount*Time.deltaTime);

}

Try that on a cube or something first, then you could probably copy the behaviour onto your ship as appropriate. Let me know if the behaviour you're getting persists. Incidentally, I would advise against just taking a script to solve your problem.

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 Averlion · Jun 22, 2010 at 04:04 PM 0
Share

this script doesnt work really well. It rotates only 50 degrees. But i want a complete rotation and more. Like Freelancer or X. I tried to delete

rollAmount *= 0.9;

but it makes problems when it gets over 180 or -180

avatar image Novodantis 1 · Jun 22, 2010 at 04:47 PM 0
Share

Sounds like you are setting the angle rather than rotating by the value. This script shouldn't have any boundries like that, I will analyse it further when at home

avatar image Novodantis 1 · Jun 22, 2010 at 10:52 PM 0
Share

Nope... it goes all the way around for me. Try attaching the above script to a cube in a blank project, you will see what I mean. Perhaps another value is being set rather than incremented? (ie pitch or yaw)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Camera rotation around player while following. 6 Answers

Enemy Pirate Ship AI 1 Answer

How do i keep an object from rotating? 2 Answers

How to rotate ship smoothly? 2 Answers

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 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