• 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 UndividedKira · Apr 01, 2020 at 11:49 AM · rigidbodybeginnerplayer movementrigidbody.velocity

How do i make local velocity?,How do i make velocity move the player the way the object is facing?

Hi I'm trying to make a player movement script using rigidbodies.I'm also pretty new to this so sorry if I have some mistakes or weird things with my code.I've decided to use velocity to move my player and I've set it up as well as camera rotation based on the mouse axis. (This also rotates the player)

But I noticed that even though my player rotates the way that I want him he doesn't really move the way he is facing. So bottom line is I was wondering how do I make him move locally based on the direction he is facing?

Here is my code:

 // Sets the rigidbody for the player

 public Rigidbody rb;

 // Sets a modifiable player speed

 public float speed = 0.0f;

 // Makes a empty vector for movement for forward,back,left,and right

 public Vector3 movement;

 // Checks if player is grounded

 public bool isGrounded;

 // calls the camera

 public GameObject icu;

 // Mouse sensitivity

 public float sensitivity = 0f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     // sets the movement and direction based on the axis of the keys

     movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

     if (Input.GetAxis("Mouse X") < 0)
     {

         transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse X") > 0)
     {

         transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") < 0)
     {

         icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") > 0)
     {

         icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);

     }

 }

 // Used for physics updates
 void FixedUpdate()
 {

     // Calls the module for player movement and sends our movement variable to said module

     playerMovement(movement);


 }

 // Makes a separate module for player movement and takes our module and sets it to help move the player in that direction

 void playerMovement(Vector3 direction)
 {

     rb.velocity = direction * speed;
 }

I was also curious if perhaps there was a good way to add a jump to the way I'm doing this?

,Hi, I'm new to most of this stuff but I'm using a rigidbody for my player and I've got it set up where the camera and object move based on the mouse axis. I also have it set up to where my player moves with velocity but it looks like it only moves globally. How can I change this to work local to the player's direction?

Here is my code:

 // Sets the rigidbody for the player

 public Rigidbody rb;

 // Sets a modifiable player speed

 public float speed = 0.0f;

 // Makes a empty vector for movement for forward,back,left,and right

 public Vector3 movement;

 // Checks if player is grounded

 public bool isGrounded;

 // calls the camera

 public GameObject icu;

 // Mouse sensitivity

 public float sensitivity = 0f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     // sets the movement and direction based on the axis of the keys

     movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

     if (Input.GetAxis("Mouse X") < 0)
     {

         transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse X") > 0)
     {

         transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") < 0)
     {

         icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") > 0)
     {

         icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);

     }

 }

 // Used for physics updates
 void FixedUpdate()
 {

     // Calls the module for player movement and sends our movement variable to said module

     playerMovement(movement);


 }

 // Makes a seprate module for player movement and takes our module and sets it to help move the player in that direction

 void playerMovement(Vector3 direction)
 {

     rb.velocity = direction * speed;
 }
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
0

Answer by tormentoarmagedoom · Apr 01, 2020 at 02:15 PM

Hello.

There are 2 ways of calculate vectors in Unity, Local and World

If you calculate the movement vector using World coordinates, player will olways move same direction regardless its rotation.

 Vector3 direction = new Vector3(1,0,0);

But if you calculate it using Local coords, the vector ("ahead") is where the object is face (the blue arrow on the Editor)

 Vector3 direction = SomeObject.transform.forward

https://docs.unity3d.com/ScriptReference/Transform-forward.html

Bye!

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.

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

179 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

Related Questions

Rigid Body Velocity max 1 Answer

how to have a fixed movement using rigidbodies without animations 0 Answers

Rigidbody2D adding force vs modifying velocity for character jump 1 Answer

Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers

Ball is not Rolling 0 Answers

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