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 Musty · Jun 25, 2014 at 03:02 PM · movementdirectioncharacter controller

Get which way the player is moving from character controller.move

Hey guys,

I was wondering whats the best way to get which way the character is moving for example is it moving back or forward or strafing left or right.

I tried using if(MoveVector.x <1)

But the problem with that is the player rotates and faces where the camera is facing and that will make it not valid.

This is how im moving my player at the moment.

 void GetLocomotionInput()
     {
         var deadZone = 0.1f;
 
         TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
 
         TP_Motor.Instance.MoveVector = Vector3.zero;
 
         if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical")< -deadZone)
         {
             TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
         }
 
         if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal")< -deadZone)
         {
             TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         }
 
     }

Thanks in Advance.

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
0

Answer by MikeNewall · Jun 25, 2014 at 03:35 PM

You can determine the direction based on which keys are pressed.

         var x = Input.GetAxisRaw("Horizontal");
         var z = Input.GetAxisRaw("Vertical");
 
         if(x > deadZone) Debug.Log("Moving right");
         if(x < -deadZone) Debug.Log("Moving Left");
         if(z > deadZone) Debug.Log("Moving Forward");
         if(z < -deadZone) Debug.Log("Moving Backward");

Comment
Add comment · Show 3 · 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 fafase · Jun 25, 2014 at 03:39 PM 1
Share

The guy is rotating so I would guess that won't do in this case.

avatar image MikeNewall · Jun 25, 2014 at 07:57 PM 0
Share

I thought he meant relative to the character not The world.

avatar image Musty · Jun 25, 2014 at 08:13 PM 0
Share

Yea sorry im rotating the character around :/

avatar image
0

Answer by fafase · Jun 25, 2014 at 03:38 PM

Use the world forward vector and compare the player forward vector:

 float dot = Vector3.Dot(transform.forward, Vector3.forward);
 if(dot > 0.9) // going forward direction
 else if (dot < - 0.9) // going opposite to forward direction
 else{
      Vector3 cross = Vector3.Cross(transform.forward, Vector3.forward);
      // This could be the other way around...never remember which order
      if(cross.y < 0) // going right 
      else // going left 
 }





Comment
Add comment · Show 3 · 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 Musty · Jun 25, 2014 at 08:13 PM 0
Share

Thanks for the reply, Would that get you going diagonals? I need it to play the right animation.

avatar image fafase · Jun 26, 2014 at 03:14 AM 0
Share

If you want diagonals you need to add it. if dot is close to 0.7 (45 degrees) or -0.7 then you are in diagonals. The value of dot is the angle in radians. Then you can just look up a radians table to see where the guy is heading. Using the cross product you get the polarity of the angle.

avatar image Musty · Jun 26, 2014 at 11:42 AM 0
Share

Alright thanks ill try that out :)

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

21 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

Related Questions

How can I move character controller in one direction 0 Answers

Transform position of an object at particular angle/direction 3 Answers

Rotation and movement? 1 Answer

Character controller, player y changes when pushing on colliders 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 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