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 /
  • Help Room /
avatar image
0
Question by OtreX · Nov 26, 2015 at 05:36 PM · rotationflyingz-axis

flying and using Z rotation (repetitive, but I didnt find an answer for this)

Hello Unity fans and thank you for spending some of your time.

My question is regarding the rotation in the Z axis. I have a space shooter in which the ships follow each other. The IA does it by using this code:

 //CurrentTarget is the gameObject that they are currently following;
 //AngularSpeed is a multiplier of the rotation. Think of it as the ship maneuvrability;
 
 gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.LookRotation(CurrentTargetTransform.position - transform.position), AngularSpeed * Time.deltaTime);
 gameObject.transform.position = gameObject.transform.position + gameObject.transform.forward * CurrentSpeed * Time.deltaTime;

So basically they are like homing missiles.

Then the player controlled ship uses the mouse and it basically flies using this code:

 transform.position = transform.position + transform.forward * Time.deltaTime * CurrentSpeed;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation (ray.direction), AngularSpeed * Time.deltaTime);

Explained in a simple way, the ship will rotate and follow the cursor (refer to this for more details: http://answers.unity3d.com/questions/718993/how-to-rotate-game-object-with-mouse-not-the-typic.html )

Now I want to add some Z rotation to that using a slerp. The ship will smoothly rotate its Z axis towards the pointer or towards its CurrentTarget. The faster that the ship is rotating on the Y axis, the faster it will rotate in its Z axis. Maybe I'm not being clear with my words as I dont know how to phrase this so well. Basically I want this: https://www.youtube.com/watch?v=wVe31hEVlJI

The Z rotation doesnt controls where the ship goes, it just add some dynamism and make things look better.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toromano · Nov 27, 2015 at 11:36 AM

I guess you need something like this:

 float LeanSpeed = .05f;
 transform.position = transform.position + transform.forward * Time.deltaTime * CurrentSpeed;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 Quaternion Direction = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(ray.direction), AngularSpeed * Time.deltaTime);
 //move direction to local
 Vector3 localDirection = transform.InverseTransformVector(ray.direction);
 Quaternion Leaning = Quaternion.AngleAxis(-localDirection.x * LeanSpeed, transform.forward); // use x component as an angle input
 transform.rotation = Direction * Leaning;

I hope it helps. Needs some fine tuning though with the LeanSpeed multiplier

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 OtreX · Nov 29, 2015 at 06:41 AM

@toromano Thanks for your time. It kind of works. the problem I get is... lets say that I'm attempting an U-turn, it will only go 90 degrees (more or less), then it will stop and the ship will begin to point up. It is worth of mentioning that I have Children gameobjects with colliders, two of them with rigidbodies. I deleted the gameObjects that contain all the colliders and rigidbodies from the ship that I use for testing and i got the same result. I'm playing around with it, if I get something I'll post it here. Thank you for your time :)

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

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

37 People are following this question.

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

Related Questions

Passing Vector3 Values to RectTransform.Rotation 1 Answer

Tilt object based on movement and direction 1 Answer

How to limit only one axis rotation? c# 1 Answer

2D z rotation in specific angel / launching object 0 Answers

Flying in fixed orbit, could use some pointers! 0 Answers


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