Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Shadowraix · Jan 11, 2014 at 01:25 PM · rotationdirection

C# Rotating object based on movement direction

I'm trying to finalize the coding for how characters will move and react with the camera. Currently, it moves depending on the directions the Camera is facing. (Code is provided for explanation) I've been searching around and could never find a solid answer on how to get the object to rotate and face the direction it is moving in since the object's rotation doesn't affect the direction it is moving in. Rotating the object is just so characters will face the direction they are moving. (Rotation is a long the Y axis based on movement of the X and Z axis.) I tried using Quaternion.LookRotation(rigidbody.velocity) but a few issues arise from that. 1. When changing movement direction, the character will instantly turn to the targeted direction. It isn't a gradual rotation change in which how a character would realistically turn. 2. When you stop moving or turning the character won't stay in the last position it was in. It will change its rotation back to normal rather than when the character stops moving the rotation stays as is. Also not moving will spam the console log saying the vector is currently 0.

Any tips, answers, or guidance towards is thanked in advance. Here is the code.

     void Update () 
     {
         if (!IsDead)
         {
             Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
             forward.y = 0;
             forward = forward.normalized;
             Vector3 right = new Vector3(forward.z,0,-forward.x);
             MovementDirection = (Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward)* WalkSpeed;
             transform.rigidbody.velocity = new Vector3(MovementDirection.x,rigidbody.velocity.y,MovementDirection.z);
             if (Input.GetMouseButton(1))
             {    //ignore this. Old rotation code not being used.
                 //transform.rotation = Quaternion.Euler(0,Mathf.LerpAngle(transform.rotation.eulerAngles.y,Camera.main.transform.eulerAngles.y,RotateDampening * Time.deltaTime),0);
 
             }
             else
             {
 
             }
         }
 
     }
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 Invertex · Jan 11, 2014 at 02:07 PM 0
Share

For 1, it's because you're not using $$anonymous$$athf.Lerp to gradually change the value. Or ins$$anonymous$$d of using $$anonymous$$athf.Lerp, you could merely make the amount rotated a lot smaller so that the person has to hold the key down for longer to reach the desired turned rate. But if you want set amount of turned degrees each time the key is pressed, then $$anonymous$$athf.Lerp would be the way to go, (as well as putting it inside an Input.Get$$anonymous$$ouseButtonDown.

1 Reply

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

Answer by Shadowraix · Jan 11, 2014 at 02:10 PM

This has been resolved. This is how it was handled.

 if (transform.rigidbody.velocity != Vector3.zero)
             {
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(new Vector3(transform.rigidbody.velocity.x,0,transform.rigidbody.velocity.z)), Time.deltaTime * RotateSpeed);
             }
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

19 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

Related Questions

rotate vector around local axis 2 Answers

How to make a player rotate in a direction with a slope of the ground. 0 Answers

How to have an object moving to one direction and rotate it whitous affecting the direction 1 Answer

Cant get my character to rotate properly 0 Answers

How can I move an object to click point in 2D? 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