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 SunilPalan · Sep 02, 2017 at 02:50 PM · c#scripting beginner

How to get my player to face the direction its moving?

Im making a 3d game that involves my player running in different directions. My controller is a joystick. I need a way to get my player to face the direction its moving in. At the moment, it's face foward for all the directions. Please help me. A script would help because I am still a beginner at coding. Thanks in advance!

 public class Mymovement : MonoBehaviour {
 
     public float moveSpeed;
     public VirtualJoystick joystick;
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
         transform.Translate(joystick.inputVector.x * moveSpeed * Time.deltaTime, 0, joystick.inputVector.z * moveSpeed * Time.deltaTime);  
     }
 }

 
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
1

Answer by TheSOULDev · Sep 02, 2017 at 03:25 PM

As a programmer, it is your job to come up with a solution, we can only show you the direction towards the right path. As for how you would get your player to face the direction in which it is moving, you will either need to match the rotation property with the velocity and/or look vector or with the input vector (the latter faces difficulty when moving in 3rd space as joystick input is a 2 dimensional vector, meaning you'd actually have to take into account a second joystick, and 2 things are harder than sometimes 2).

As for how you implement this (animation, state etc.) is completely up to you. For a 2D game you can simply use cotangent / tangent calculation to index 2^n-axis directions as you won't have infinite sprites for infinite angles. For a 3D game, you'd use the purely vector approach.

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
1

Answer by spidermancy612 · Sep 02, 2017 at 03:55 PM

You'll want to use the same inputs from your movement to also determine the rotation for the object. So for example you can have a Horizontal input of 1 and a Vertical input for -1. In this case we have the player moving right and down at a 45 degree angle, we'll also want the player angled at 45 degrees to match. You can create a new vector for where the player is moving and set the rotation to that object.

  float moveHorizontal = Input.GetAxisRaw ("Horizontal");
  float moveVertical = Input.GetAxisRaw ("Vertical");
   
  Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  transform.rotation = Quaternion.LookRotation(movement);
   
  transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);

This answer was provided by @PaxForce in another thread so I just stole the code example he provided to give an idea for how to accomplish this.

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

69 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to call child overide function? 1 Answer

(URGENT) I have a swinging axe I can get to rotate once in 1 direction but i need it to loop 3 Answers

Unbinding in game settings 1 Answer


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