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 jhehey99 · Apr 13, 2017 at 04:18 PM · c#beginnertransform.rotate

Problem with rotation Please Help Me

I have a GameObject "Player" that is the parent of an Image "The Red". I use the Player for the movement. and The Red for the rotation. When I press, WASD keys, it will move and rotate towards where it is moving. When I rotate from 0 degrees to 270 degrees, there is no problem, it rotates counter clockwise. but when I rotate from 270 degrees to 360 degrees then it will rotate clockwise instead of counter clockwise which is closer. I think there is something that I need to take the difference of but I don't know what.

This is what it looks like from the Game.

https://media.giphy.com/media/l4FGkuEF9bk2dWk1O/giphy.gif

My GameObjects from the Inspector

This is my PlayerMovement.cs Script

public class PlayerMovement : MonoBehaviour { public float moveSpeed; public GameObject theRed;

 void Update()
 {
     Vector3 movement = new Vector3 ();
     Vector3 rotation = Vector3.zero;

     if(Input.GetKey(KeyCode.W))
     {
         movement += Vector3.up;
         rotation.z = 90f;
         theRed.transform.localEulerAngles = Vector3.Slerp(theRed.transform.localEulerAngles, rotation, 0.15f);
     }    
     else if(Input.GetKey(KeyCode.S))
     {
         movement += Vector3.down;
         rotation.z = 270f;
         theRed.transform.localEulerAngles = Vector3.Slerp(theRed.transform.localEulerAngles, rotation, 0.15f);
     }    
     else if(Input.GetKey(KeyCode.A))
     {
         movement += Vector3.left;
         rotation.z = 180f;
         theRed.transform.localEulerAngles = Vector3.Slerp(theRed.transform.localEulerAngles, rotation, 0.15f);
     }    
     else if(Input.GetKey(KeyCode.D))
     {
         movement += Vector3.right;
         rotation.z = 0f;
         theRed.transform.localEulerAngles = Vector3.Slerp(theRed.transform.localEulerAngles, rotation, 0.15f);
     }

     transform.Translate (moveSpeed * movement.normalized * Time.deltaTime);
 }

}

scene.png (45.8 kB)
Comment
Add comment · Show 3
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 jhehey99 · Apr 13, 2017 at 03:54 PM -1
Share

pls help me

avatar image jhehey99 · Apr 13, 2017 at 04:42 PM -1
Share

HELP!!!!!!!

avatar image jhehey99 · Apr 13, 2017 at 07:26 PM 0
Share

Update: I finally fixed it and got it working the way i wanted to. I'm using WASD keys for the movement. Thank you sirs!

Here is what is looks like: https://media.giphy.com/media/3ohzdJvzwjh$$anonymous$$lEUAbC/giphy.gif

Here is the code in case anyone runs into the same problem.:

public class $$anonymous$$OVE : $$anonymous$$onoBehaviour { public float moveSpeed; public GameObject theRed;

 void Update ()
 {
     float moveH = Input.GetAxisRaw ("Horizontal");
     float moveV = Input.GetAxisRaw ("Vertical");
     Vector3 movement = new Vector3 (moveH, moveV, 0);

     transform.Translate (movement.normalized * moveSpeed * Time.deltaTime);

     if(movement != Vector3.zero)
     {
         float angle = $$anonymous$$athf.Atan2 (moveV, moveH) * $$anonymous$$athf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward);
         theRed.transform.rotation = Quaternion.Slerp (theRed.transform.rotation, rotation, Time.deltaTime * 5f);
     }

 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Apr 13, 2017 at 04:43 PM

Well first, get rid of the slerps. And since you're only editing one component of the rotation, I would store it as a float instead of a Vector3.

What you want to do is manually change the rotation value. If you have two variables, CurrentRotation and TargetRotation, you can do something like this:

 float rotDiff = (TargetRotation-CurrentRotation);
 while(rotDiff < -180)
     rotDiff += 360;
 while(rotDiff > 180)
     rotDiff -= 360;
 CurrentRotation += rotDiff * Time.deltaTime;
 transform.rotation = Quaternion.Euler(0,0,CurrentRotation);

That will ensure it's always going in the shortest direction, and it allows you to track the rotation more easily.

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 jhehey99 · Apr 13, 2017 at 07:15 PM 0
Share

Thank you sir for the advice

avatar image RibsNGibs · Apr 13, 2017 at 08:32 PM 1
Share

If you store a float representing an angle as opposed to storing a Quaternion, you can go back to using lerps if you like (although it doesn't look like he was using it correctly) using the handy $$anonymous$$athf.LerpAngle() function which does the right thing when the angles wrap around 360/0. https://docs.unity3d.com/ScriptReference/$$anonymous$$athf.LerpAngle.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[RESOLVED! :D][C#] Friction/Deceleration - Compiler won't let me damp individual Rigidbody axes to 0. Force in opposite direction maybe? 2 Answers

C# for beginners in Unity for multi-platforms? 1 Answer

How do I update the position of different prefabs to the current position? 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