• 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 xthunderduckx · Nov 25, 2019 at 01:41 AM · physics3dcharactercontrollervelocitytransformdirection

How to transform velocity to world space before calling controller.move?

In my game I am trying to make it so that the player's velocity increases based on the direction the camera is facing. This is simple enough to do, by calling controller.move(transform.TransformDirection(velocity)), but there is the issue that if the player then rotates the camera, the velocity will again be converted, causing the player to swerve. I'm trying to make it so that, for example, when the player presses "W," they will increase speed in the direction that the camera is facing, at the exact instance the key is pressed.


This is what I presently have; as you can see whenever the player presses a movement key, their velocity increases by the respective amount, but that value is transformed the instant that the key is pressed instead of waiting for the .move(velocity) to be called. I did this in hopes that the player would continue travelling in the same direction that the velocity would be regardless of the gameobject's rotation. This works as intended when the player has a rotation of 0,0,0, but I get some extraneous results when changing rotation. How can I improve my movement script?

 private void movePlayer()
     {
         if (playerController.isGrounded)
         {
             if (Input.GetKey(KeyCode.Space))
             {
                 velocity.y = 10f;
             }
             if (Input.GetKey(KeyCode.W))
             {
                 if (velocity.z < maxSpeed)
                 {
                     velocity += player.transform.TransformDirection(0, 0, (accelerationSpeed * Time.deltaTime));
                     if(velocity.z > maxSpeed) { velocity.z = maxSpeed; }
                 }
             }
             if (Input.GetKey(KeyCode.S))
             {
                 if(velocity.z > -maxSpeed)
                 {
                     velocity -= player.transform.TransformDirection(0, 0, (accelerationSpeed * Time.deltaTime));
                     if (velocity.z < -maxSpeed) { velocity.z = -maxSpeed; }
                 }
             }
 
             if (Input.GetKey(KeyCode.A))
             {
                 if(velocity.x > -maxSpeed)
                 {
                     velocity -= player.transform.TransformDirection((accelerationSpeed * Time.deltaTime), 0, 0);
                     if (velocity.x < -maxSpeed) { velocity.x = -maxSpeed; }
                 }
             }
 
             if (Input.GetKey(KeyCode.D))
             {
                 if(velocity.x < maxSpeed)
                 {
                     velocity += player.transform.TransformDirection((accelerationSpeed * Time.deltaTime), 0, 0);
                     if (velocity.x > maxSpeed) { velocity.x = maxSpeed; }
                 }
             }
             applyFriction();
         }
         if (!playerController.isGrounded)
         {
             velocity.y -= 25f * Time.deltaTime;
         }
         playerController.Move(velocity * 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
Best Answer

Answer by Ermiq · Nov 25, 2019 at 09:52 AM

So, you want to simulate inertia, right? Maybe something like this?


Update: previous version was all wrong actually.

 Vector3 desiredDirection;
 Vector3 velocity;
 float inputX;
 float inputY;
 
 void Update() {
     inputX = Input.GetAxis("Horizontal"); // A D input
     inputY = Input.GetAxis("Vertical"); // W S input
     
     // get desired move direction relative to camera or transform direction and WASD inputs
     desiredDirection = (transform.right * inputX + transform.forward * inputY).normalized;

     // calculate the velocity the controller will get to check the resulting speed
     velocity = controller.velocity + desiredDirection * accelerationSpeed * Time.deltaTime;

     // if the speed gonna be more than maxSpeed, than just set velocity to maxSpeed at the desired direction
     if (velocity.magnitude > maxSpeed)
          velocity = desiredDirection * maxSpeed;
     
     controller.Move(velocity * Time.deltaTime);
 }

With this setup the controller should slow down when you rotate briefly and get speed on again in new direction, I guess.
But it won't stop if you just release all WASD keys. The only way to stop is to hold down S key while looking in the move direction. Or W while looking opposite.
To make the controller stop when there's no input, need to make it something like this:

  Vector3 desiredDirection;
  Vector3 velocity;
  float inputX;
  float inputY;
  
  void Update() {
      inputX = Input.GetAxis("Horizontal"); // A D input
      inputY = Input.GetAxis("Vertical"); // W S input
     
      // get desired move direction relative to camera or transform direction and WASD inputs
      desiredDirection = (transform.right * inputX + transform.forward * inputY).normalized;

      if ((inputX = 0 && inputY = 0) //released WASD
      {
         velocity = Vector3.zero;
      }
      else // if at least one of WASD keys is down
      {
          // calculate the velocity the controller will get to check the resulting speed
          velocity = controller.velocity + desiredDirection * accelerationSpeed * Time.deltaTime;
         
          // if the speed gonna be more than maxSpeed, than just set velocity to maxSpeed at the desired direction
          if (velocity.magnitude > maxSpeed)
               velocity = desiredDirection * maxSpeed;
     }
      
      controller.Move(velocity * Time.deltaTime);
  }

Comment
Add comment · Show 1 · 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 xthunderduckx · Nov 25, 2019 at 03:09 PM 0
Share

I ended up asking some of my friends and we talked through the process and figured it out, but this is roughly the same conclusion we got to. Thanks.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

How to move while jumping? 1 Answer

Same Collision Rebound Force on Cars 0 Answers

How to make my Character run through walls with the use of the Character Controller component 1 Answer

CharacterController causing characters to fly around screen 1 Answer

Removing velocity relative to the contact normal 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges