- Home /
Question by
Phantom07 · Aug 07, 2019 at 04:01 PM ·
c#rigidbodyplayermovement scripttransform.position
Move Multiple Directions At Once (ex. forward and right) unity 3d
I made a script to use wasd to move. the problem is i cant move in multiple directions at once. (ex. up and to the left)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public Rigidbody PlayerRb;
public float movementSpeed = 5.0f;
void FixedUpdate()
{
if(Input.GetKey(KeyCode.W)) {
transform.position += transform.forward * Time.deltaTime * movementSpeed; // forward
}
if(Input.GetKey(KeyCode.S)) {
PlayerRb.position += transform.forward * Time.deltaTime * -movementSpeed; // backward
}
if(Input.GetKey(KeyCode.A)) {
PlayerRb.position += transform.right * Time.deltaTime * -movementSpeed; // left
}
if(Input.GetKey(KeyCode.D)) {
PlayerRb.position += transform.right * Time.deltaTime * movementSpeed; // right
}
}
}
Comment
Best Answer
Answer by me-der · Aug 07, 2019 at 04:25 PM
I would try to change all "PlayerRb" to "transform" as you did in walking forward, then you should be able to walk in 8 directions.
Ok. I think i figured it out. I'll try this if my idea doesn't work. :)
Your answer
Follow this Question
Related Questions
Rigidbody movement script 2 Answers
How to determine the direction of an object to the player. 1 Answer
Getting the Player in a Script! 2 Answers
Gravity and rotation control 0 Answers
Player rotation restricted even though I don't want it to be 0 Answers