Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do you rigidbody.MovePosition (); in 2 directions at the same time?

Discussion in 'Scripting' started by EliteWalrus, Dec 14, 2014.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Here is what I've tried;
    Code (CSharp):
    1.     void FixedUpdate () {
    2.         rigidbody.MovePosition (transform.position + transform.forward * Input.GetAxis ("Vertical") *               movementSpeed * Time.deltaTime);
    3.         rigidbody.MovePosition (transform.position + transform.right * Input.GetAxis ("Horizontal") * movementSpeed * Time.deltaTime);
    4.     }
    5. }
    6.  
    Only horizontal movement works.
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Could you do "transform.forward + transform.right * Input.ect......"
     
    EliteWalrus likes this.
  3. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    That happens because you are overriding the vertical movement with the horizontal one.
    You have to do as RiokuTheSlayer says. First find your movement vector, then apply it to rigidbody.MovePosition.
     
    EliteWalrus likes this.
  4. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    Anyways, what are you using MovePosition for? Is it a character controller or you want to get some kind of custom behaviour?

    I believe there are better and easier ways to get character movement, that's why i'm curious :D
     
  5. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Thanks guys :)

    I'm just trying to keep everything clean, I don't want all kinds of components and scripts lying around everywhere. I also get some personal satisfaction knowing that I scripted my own movement, simple as it may be.
     
    Rienhl likes this.