Search Unity

Getting The Horizontal And Vertical Velocities As Separate Floats?

Discussion in 'Scripting' started by Torik_, Feb 7, 2016.

  1. Torik_

    Torik_

    Joined:
    Jan 11, 2016
    Posts:
    4
    Hey,
    Im trying to make a simple 2d sprite move and jump around. The jumping mechanics are working fine but I cannot implement the movement into the script without making the jumping affect the movement and the movement affect the jumping. Is there a way to get the velocity of the horizontal axis and input it into UNSURE1 and get the velocity of the vertical axis and input it into UNSURE2. I put them as UNSURE so it would be easy to see where i wasnt sure what to add.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerJumper : MonoBehaviour {
    5.  
    6.     private Rigidbody2D rb;
    7.  
    8.     public float Speed;
    9.  
    10.     public float Jump_Power;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.         rb = GetComponent<Rigidbody2D> ();
    16.  
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         print(Input.GetAxisRaw("Vertical"));
    22.     //    rb.AddForce (new Vector2 (Input.GetAxis ("Horizontal")*Speed*Time.deltaTime,0));
    23.  
    24.         if (Input.GetKeyDown (KeyCode.UpArrow)) {
    25.             rb.velocity = new Vector2 (UNSURE1, Jump_Power);
    26.         } else {
    27.             rb.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * Speed * Time.deltaTime, UNSURE2);
    28.         }
    29.     }
    30. }
    31.  
     
  2. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    1. Do you mean this?

    2. Code (csharp):
      1.  
      2. if (Input.GetKeyDown (KeyCode.UpArrow)) {
      3.        rb.velocity = new Vector2 (rb.velocity.x, Jump_Power);
      4. } else {
      5.        rb.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * Speed * Time.deltaTime, rb.velocity.y);
      6. }
      7.  
     
  3. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    I don't quite know what you are trying to do with your code, but here's how to find the velocity of a rigidbody on a particular axis. You can do the rest :)

    Code (CSharp):
    1. float horizontalVelocity = Vector2.Dot(rigidbody.velocity, Vector2.right);
    2.  
    3. float verticalVelocity = Vector2.Dot(rigidbody.velocity, Vector2.up);
     
    SevenG3P likes this.
  4. Torik_

    Torik_

    Joined:
    Jan 11, 2016
    Posts:
    4
    Excellent thankyou so much! I had no idea how to find the velocities so to think its as easy as that! xD
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That's true, but this is mathematically equivalent and much simpler:

    Code (CSharp):
    1. float horizontalVelocity = rigidbody.velocity.x;
    2. float verticalVelocity = rigidbody.velocity.y;
     
    Kiwasi likes this.
  6. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Yeah I know, I'm just used to working in 3D where a non world axis might be needed, so it's good to know!
     
    JoeStrout likes this.