- Home /
 
How to move rigidbody Diagonal with movePosition
 var player :Transform;
 var speed = 10;
 private var direction = Vector3(0,0,0);
 
 private var grounded = 0;
 
 function Update () {
 walking = speed  * Time.deltaTime;
 
 
 
 if(Input.GetKey("w"))
 player.rigidbody.MovePosition(player.position + player.forward * walking);
 
 if(Input.GetKey("d"))
 player.rigidbody.MovePosition(player.position + player.right * walking);
 
 if(Input.GetKey("a"))
 player.rigidbody.MovePosition(player.position + player.right * -walking);
 
 if(Input.GetKey("s"))
 player.rigidbody.MovePosition(player.position + player.forward * -walking);
 
 if(Input.GetKey("w") && (Input.GetKey("d")))
 player.rigidbody.MovePosition(player.position + player.forward.right * Time.deltaTime);
 
 
 
 
 }
 
               there's my whole script so far, and i have seen other for controlling rigidbodys, but i dont what to have a script i cant understand so im making my own, i don't want to use add Force, but if i dont then there is no diagonal its either straight or sideways. how do i make a dagonal?
It's a lot easier if you use GetAxis ins$$anonymous$$d of a bunch of keys.
For some reason my reply didn't show up, despite the message saying that it was waiting for approval dissapearing.
Anyway, it was basically this. You can declare a temporary Vector3 called movement. WASD adds +1 in the direction the buttons correspond to to this vector.
then normalize the vector and multiply it with your speed. And then use moveposition. $$anonymous$$uch simpler than adding && comparisons for every possible button combination.
Answer by KungKras · Jan 07, 2016 at 10:26 PM
 Vector3 movement;
 
 if (Input.GetKey (KeyCode.W)) {
     movement = movement + transform.forward;
 }
 if (Input.GetKey (KeyCode.S)) {
     movement = movement - transform.forward;
 }
 if (Input.GetKey (KeyCode.A)) {
     movement = movement + transform.TransformDirection (Vector3.left);
 }
 if (Input.GetKey (KeyCode.D)) {
     movement = movement + transform.TransformDirection (Vector3.right);
 } 
 movement = Vector3.Normalize (movement);
 movement = movement * speed;
 rigidbody.MovePosition (transform.position + movement*Time.fixedDeltaTime);
  
 
              Answer by DaveA · Jun 12, 2011 at 04:42 PM
Accumulate a 'delta' in a Vector3, then apply it with MovePosition at the end
Could you help me understand that better, i am kind of a noob in scripting, also if it is a Vector3, wouldn't it be the world-wide x,y,z coordinates, they need to be local.
Yes, please help me understand what you are saying!
Answer by Siloaman · Jan 06, 2016 at 03:45 PM
using UnityEngine; using System.Collections;
public class rigidbody_controller_script : MonoBehaviour {
 Rigidbody rb;
 float moveSpeed;
 
 public Vector3 straightForward;
 
 public Quaternion spreadAngleRight;
 public Quaternion spreadAngleLeft;
 
 public Vector3 localRightVector;
 public Vector3 localLeftVector;
 
 public Vector3 newVectorRight;
 public Vector3 newVectorLeft;
 void Start () {
 
     moveSpeed = 20.0f;
     
 
 
     straightForward = transform.TransformDirection(Vector3.forward);
     
     spreadAngleRight = Quaternion.AngleAxis(45, new Vector3(0,1,0));
     spreadAngleLeft = Quaternion.AngleAxis(315, new Vector3(0,1,0));
     
     localRightVector = spreadAngleRight * straightForward;
     localLeftVector = spreadAngleLeft * straightForward;
     
     newVectorRight = transform.TransformDirection(localRightVector);
     newVectorLeft = transform.TransformDirection(localLeftVector);
     rb = GetComponent<Rigidbody>();
 
 }
 
 void Update () {
 
 
 
 // REGULAR WASD COMMANDS
 
     if(Input.GetKey(KeyCode.W))
         rb.MovePosition(transform.position + transform.TransformDirection(Vector3.forward * moveSpeed) * Time.deltaTime);
                                                                                             
     if(Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position + transform.TransformDirection(-Vector3.right * (moveSpeed/2) ) * Time.deltaTime);
         
     if(Input.GetKey(KeyCode.S))
         rb.MovePosition(transform.position + transform.TransformDirection(-Vector3.forward * (moveSpeed/4) ) * Time.deltaTime);    
         
     if(Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position + transform.TransformDirection(Vector3.right * (moveSpeed/2) ) * Time.deltaTime);
     
 // STRAFING COMMDANDS
     
     if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position + transform.TransformDirection(newVectorRight * moveSpeed) * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position + transform.TransformDirection(newVectorLeft * moveSpeed) * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
         rb.MovePosition(transform.position - transform.TransformDirection(newVectorLeft * (moveSpeed/4) ) * Time.deltaTime);
         
     if(Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
         rb.MovePosition(transform.position - transform.TransformDirection(newVectorRight * (moveSpeed/4) ) * Time.deltaTime);
 
 
 }
 
               }
Your answer
 
             Follow this Question
Related Questions
Player Falls Over 1 Answer
Pick Up Object using Rigidbody FPS 0 Answers
Need Help in FPS controller based on Rigidbody 0 Answers