• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DaKocha · Jul 08, 2013 at 04:10 PM · movementdirection

How to get the movement direction relative to a character

How do I get the movement direction of a player of a character?

Like forward, backward, left-right etc., how do I achieve this?

I can calculate movement direction relative to the world space but not relative to the character.

For example, if the character heads north, the the movement direction would be 0 degrees, if he goes south, 180 degrees etc.

But if the player was heading north with his face facing east, then the relative movement direction would be 315 degrees etc.

Comment
Add comment · Show 2
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 supercouge · Jul 08, 2013 at 04:22 PM 0
Share

Did you look at Transform.InverseTransformDirection ?

avatar image DaKocha · Jul 08, 2013 at 04:27 PM 0
Share

Nope, what exactly does it does? Checkin out... Surely not what I was lookin for, hmm.

Edited my question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Jul 08, 2013 at 04:40 PM

the local direction is

transform.forward -transform.forward

transform.right -transform.right

transform.up -transform.up

those are vectors which give a direction which is 1 unit away from the objective relative to itself (so its 0,0,0)

its what your looking for

so if you'd like to move forward

character.charactercontroller.move(character.transform.forward * speed)

backward is

character.charactercontroller.move(-character.transform.forward * speed)

note the -sign

the - inverses it so -forward is backward, - right is left etc.

Comment
Add comment · Show 4 · 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 DaKocha · Jul 08, 2013 at 04:45 PM 0
Share

I cant seem to understand your answer :/ can you please elaborate a little bit? Like transform.forward -transform.forward = 0

avatar image sparkzbarca · Jul 09, 2013 at 12:31 PM 0
Share

no i mean

if you want to get 1 unit ahead of the player its position/direction is

 player.transform.forward

if you want to get 1 unit behind the player

ITS NOT

 player.transform.backward

that doesnt exist

IT IS

 -player.transform.forward


this is an example of a script you can attach to the main camera that will move the camera forward/backward/left right/ based on you pressing W A S D

 public class $$anonymous$$oveCamera : $$anonymous$$onoBehaviour {
     Transform cam_tran;
     public float move_speed;
     // Use this for initialization
     void Start () {
         cam_tran = Camera.main.transform;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
         {
             cam_tran.Translate(Vector3.forward * move_speed * Time.deltaTime);
         }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
         {
             cam_tran.Translate(Vector3.left * move_speed * Time.deltaTime);
         }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
         {
             cam_tran.Translate(Vector3.back * move_speed * Time.deltaTime);
         }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
         {
             cam_tran.Translate(Vector3.right * move_speed * Time.deltaTime);
         }
     }
 }

you will notice it has a backward and left. Thats because vector3 does have a backward and left although these are just equal to 0 0 1 and -1 0 0

most gameobjects do not have a left you use -right

avatar image DaKocha · Jul 09, 2013 at 12:42 PM 0
Share

Thanks for helping sparkzbarca :) , I understand that now. But after searching deeply, I found my answer somewhere in the unity answers, my bad, I didn't searched that deep before asking, sorry for bpthering you guys :/

avatar image pcdcreeper DaKocha · Apr 28, 2021 at 05:25 AM 0
Share

What was the answer?! I am so sorry to necro, but I need help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Have a character face their moving direction? 1 Answer

Relative Movement Problem 1 Answer

Making a character move automatically in one direction. 2 Answers

bullet problems 1 Answer

I am having problems with making my character face the movement direction after stopping. 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