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 Nevac · Jun 02, 2016 at 03:27 PM · rotationlogic

Rotation problem: Object rotates but shouldn't

Hello community!

I serached long in the forums for a solution and read the documentation again and again, but I'm now in a little dead end.

The situation: I have a spaceship. The spaceship should ALWAYS look to the direction it is flying. When it is not, it simply looks at the last direction it was flying. It can fly in 8 directions, with the keys W A S D and combinations. Now to bring a little bit quality in it, I want that the spaceships turns when you fly in another direction as before.

Lets have a look into the code. I have commented the important parts that causes the problem, the rest is just regular controlls stuff:

 private float InputValueMoveX;
 private float InputValueMoveZ;

 private Vector3 moveDirection;

 public int speed;
 public int rotationSpeed;
 public GameObject ship;

 public Rigidbody shipRB;

 void Start () {
 }
 
 void Update()
 {
     InputValueMoveX = Input.GetAxis("MoveX");
     InputValueMoveZ = Input.GetAxis("MoveZ");
 }

 private void FixedUpdate ()
 {
     Movement();
     Turn();
 }

 private void Movement()
 {
     float moveX = InputValueMoveX * speed * Time.deltaTime;
     float moveZ = InputValueMoveZ * speed * Time.deltaTime;

     transform.Translate(moveX, 0, moveZ);
     saveMoveDirection(moveX, moveZ); // --------THIS-------
 }

 private void Turn() // ------ THIS FUNCTION------
 {
     if (Vector3.Dot(ship.transform.right.normalized, moveDirection.normalized) != 1) // MAIN PROBLEM IN THIS LINE
     {
         float turn = rotationSpeed * Time.deltaTime;
         Quaternion rotation = Quaternion.Euler(0f, turn, 0f);
         shipRB.MoveRotation(shipRB.rotation * rotation);
     }
 }

 private void saveMoveDirection(float moveX, float moveZ) // -----THIS FUNCTION------
 {
     if(moveX != 0 || moveZ != 0)
     {
         moveDirection = new Vector3(moveX, 0, moveZ);
     }
 }

 private bool KeyDown(string key)
 {
     if (Input.GetKey(key))
     {
         return true;
     }
     
     else
     {
         return false;
     }
 }

Now, as you can see I have a function calles saveMoveDirection(float moveX, float moveZ). The only thing it does is to save the last direction the ship was flying in Vector3 moveDirection, which will be later important in the function Turn() for ship rotation. It will not be overwritten when the ship stands and the Vector is (0, 0, 0).

Now if look at Turn() we see that there is an if function and in the if function is my rotation (with Quaternions). The rotation works without problem. The main problem here is the if function.

if (Vector3.Dot(ship.transform.right.normalized, moveDirection.normalized) != 1)

That means that if the local X axis (Vector3(1,0,0)) of my ship and Vector3 moveDirection show not in the same direction and have no points align, the ship will turn.

Logically it should turn so long until both Vectors are align and show in the same direction.

Now comes the funny thing. When I use a low value for rotationSpeed (for example 90f) in Turn() everything is fine and it works. But i think that too slow to turn around. When I double this value (180f) then suddenly the ship only holds on W (0°) A (90°) S (180°) D (270°) directions, but not the combined ones (45°, 135°, 225°, 315°) as if it would do too many steps per frame so that it does not catch them. If I go even further with the values, it holds on no direction. I took also 130f once and also did not hold at any point.

Is this some mathematical thing I did not understand? I really don't see what went wrong. There is some lack, but I don't find it.

I hope I could explain my problem well, and appreciate any help I can get.

Thank you people and have all a nice day!

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

0 Replies

· Add your reply
  • Sort: 

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

65 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 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

Can't edit an object's rotation manually? 1 Answer

Rotating player around rotating planet,Rotation of player on a rotating planet 0 Answers

Calculating the Hit Point on a Sphere (with Rotation, Based on Static Nodes) 0 Answers

Unity 3D C# - Rotation To Slope Of Terrain 0 Answers

How to rotate quad towards light in shader? 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