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 /
  • Help Room /
avatar image
0
Question by zigzagskitty · Mar 05, 2021 at 02:50 AM · movementcharactercontroller

how to make player movement be based on direction its facing

I want my player to move directions relative to the direction the player is facing when I press the arrow keys. So if I press the "up" arrow to move the player forward, I want forward to be the direction it is facing, yet forward seems to always be tied to the worlds forward, because pressing the "up" arrow moves me in the same direction regardless of what way I'm facing. My code does correctly turn the player so that it is facing whichever direction it is going, making the local "forward" correct, but then pressing the "up" arrow turns my player to the Global forward instead of moving in the players forward. I'm pretty new to both coding and Unity, and I don't know how to go about changing that. Any advice or info is appreciated (even if it doesn't solve this particular problem, I'm eager to learn) but I will say I'd prefer not to use a Rigidbody, I'm trying to learn using character controller. Here is my code for the player:

public class PlayerController : MonoBehaviour { private CharacterController playerCC; private Vector3 playerVelocity; private bool groundedPlayer; private float playerSpeed = 10.0f; private float jumpHeight = 3.0f; private float gravityValue = -9.81f; [SerializeField] Camera mainCamera;

 private void Start()
 {
     playerCC = gameObject.GetComponent<CharacterController>();
 }
 void Update()
 {
     groundedPlayer = playerCC.isGrounded;
     if (groundedPlayer && playerVelocity.y < 0)
     {
         playerVelocity.y = 0f;
     }
     float horizontalInput = Input.GetAxis("Horizontal");
     float verticalInput = Input.GetAxis("Vertical");

     Vector3 move = new Vector3(horizontalInput, 0, verticalInput);
     playerCC.Move(move * Time.deltaTime * playerSpeed);

     if (move != Vector3.zero)
     {
         gameObject.transform.forward = move;
     }
     if (Input.GetButtonDown("Jump") && groundedPlayer)
     {
         playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
     }
     playerVelocity.y += gravityValue * Time.deltaTime;
     playerCC.Move(playerVelocity * 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tylerdtrudeau · Mar 05, 2021 at 05:05 AM

Im just gonna guess you wand the sideways arrow key to rotate the player? then up and down to move forward and back relative to facing direction?


 public float movementSpeed = 25f;
 
     void Update()
     {
         float hInput = Input.GetAxis("Horizontal");
         float vInput = Input.GetAxis("Vertical");
 
         if (vInput > 0)
         {
             transform.position += transform.up * Time.deltaTime *movementSpeed;
         }
         if (vInput < 0)
         {
             transform.position -= transform.up * Time.deltaTime *movementSpeed;
         }
         if (hInput > 0)
         {
             transform.Rotate(0, 0, -1f, Space.Self);
         }
         if (hInput < 0)
         {
             transform.Rotate(0, 0, 1f, Space.Self);
         }

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 zigzagskitty · Mar 06, 2021 at 12:09 AM 0
Share

Thanks, but that isn't quite what I was looking for. My game is 3D, 3rd person perspective so I need the arrows to move left, right, forward, and back, but I want those directions to be relative to the direction the player/camera is facing. Right now, "forward" is always the same direction regardless of where the player is facing.

avatar image tylerdtrudeau zigzagskitty · Mar 06, 2021 at 12:16 AM 0
Share

Well most of the code I provided should still work. The rotation should still be fine (you may need to change which axis the vector is using (0,01,) or (0,1,0) but the transform.rotate will still work in 3D space on a 3D game object (player). For the forward and back it SHOULD work as well but instead of Vector3.up, try Vector3.Forward.

avatar image tylerdtrudeau zigzagskitty · Mar 06, 2021 at 12:37 AM 0
Share

Ok I tested it out and it seems to work perfect. So here you go.


 public class Movement : MonoBehaviour
 {
     public float movementSpeed = 5f;
 
     void Update()
     {
         float hInput = Input.GetAxis("Horizontal");
         float vInput = Input.GetAxis("Vertical");
  
         if (vInput > 0)
         {
             transform.position += transform.forward * Time.deltaTime *movementSpeed;
         }
         if (vInput < 0)
         {
             transform.position -= transform.forward * Time.deltaTime *movementSpeed;
         }
         if(hInput > 0)
         {
             transform.Rotate(0, 1f, 0, Space.Self);
         }
         if (hInput < 0)
         {
             transform.Rotate(0, -1f, 0, Space.Self);
         }
     }
 }



then attach this next script onto your camera


 public class CameraFollow : MonoBehaviour
 {
     public GameObject player;
     float camDistance = 10f;
 
     void LateUpdate()
     {
         transform.position = player.transform.position - player.transform.forward * camDistance;
         transform.LookAt (player.transform.position);
         transform.position = new Vector3 (transform.position.x, transform.position.y + 5, transform.position.z);
     }
 }



go into the editor and clcik on your camera, drag your player from the hierarchy into the "player" field of the CameraFollow script and youre done.

avatar image zigzagskitty tylerdtrudeau · Mar 06, 2021 at 04:23 PM 0
Share

Thank you!!! It's not quite what I want yet, but I was able to incorporate your code to get much closer, now to fiddle around and see if I can get it just right.

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

223 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 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 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 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

Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers

is it possible to edit the Continuous Move provider (Action based) to add a jump feature? 0 Answers

How to move my rigid body in the direction my model is facing. 0 Answers

Problem with Jump script 1 Answer

i'm using a code to make my cube walk with character controller but when i play the cube spins. 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