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 BigFudge_ · Feb 06, 2016 at 03:43 PM · rotationmovement script

Rotate player

Hi, kind people of unity forums. I am having trouble with my script as to how I can rotate the player while moving so it would look like the player is actually walking in different directions rather than "strafing" I need my player to rotate so he is always facing the direction he is going to with respect to the button pressed. Much like how it would behave with a joystick. Can anyone help me visualize the code? Here's my rather simple script for player movement (with the help from one of the forum members)

 public float speed = 5; // set speed
 public float distancetoGround;
 void Start()
 {
 distancetoGround = collider.bounds.extents.y;
 }
  
  
 void Update()
 {
  
 if (Input.GetKeyDown(KeyCode.UpArrow))
 {
 Vector3.forward * speed * time.deltatime
 }
  
  
 if (Input.GetKeyDown(KeyCode.DownArrow))
 {
 Vector3.back * speed * time.deltatime
 }
  
  
 if (Input.GetKeyDown(KeyCode.LeftArrow))
 {
 ]Vector3.left * speed * time.deltatime
 }
 if (Input.GetKeyDown(KeyCode.RightArrow))
 {
 Vector3.Right * speed * time.deltatime
 }
  
  
 if (Input.GetKeyDown(KeyCode.Space)) && isGrounded())
 {
 // do jump // assuming you want to check if you can jump
 }
  
  
 }
  
  
  
  
 public bool isGrounded(bool checkGrounded)
 {
 return Physics.Raycast(transform.position,-Vector3.up, distToGround +0.1);}
 }
  
  

Any help will be much appreciated.

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 Glurth · Feb 06, 2016 at 04:40 PM 0
Share

this code does not look complete: for example, what is happening on line 14:

 Vector3.forward * speed * time.deltatime

To make your character face the direction you are moving, try something like this in update:

 transform.rotation= Quaternion.LookRoatation(velcocityVector3.normalized);

(not sure how you are storing current velocity, so I just used "velocityVector3")

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing the direction of movement 0 Answers

Character Rotation 0 Answers

Cube turns in moving direction, but always returns to default direction 0 Answers

Fixed Update randomly stops player rotation 0 Answers

Top-Down Movement and Rotation Toward Joystick Direction 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