- Home /
Simple Third Person Camera-Relative Movement with WASD - kind of working but need a fix
Hi, So I am trying to get my player to move by adding a force to its rigidbody. I have a third person camera script that allows me to freely move the camera around the player using the mouse. In my player movement script below, I need the movement of the player to correspond with the direction the camera is facing. So far, the player moves in the direction the camera is facing, but I cannot control it with WASD. I cannot find a way to implement GetAxis into the movement calculation. Any help is appreciated :)
using System; using System.Collections.Generic; using UnityEngine;
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed = 12;
 
     Rigidbody rigidBody;
     float moveX;
     float moveZ;
 
     bool isTouching = false;
     public float jumpPower;
 
     public Transform _camera;
 
     void Start () {
         rigidBody = this.GetComponent<Rigidbody> ();
 
         if (rigidBody == null)
             Debug.LogError ("RigidBody could not be found.");
     }
 
     void Update () {
         moveX = Input.GetAxis ("Horizontal");
         moveZ = Input.GetAxis ("Vertical");
     }
 
     void FixedUpdate () {
 
         Vector3 cameraDirection = transform.position - _camera.position; //direction to move in is equal to the Vector3 between camera and player (direction camera is facing)
         cameraDirection.y = 0.0f; //zeros the vertical aspect of movement direction to stay in the x-z plane
         if (rigidBody != null) {
             rigidBody.AddForce (cameraDirection * moveSpeed, ForceMode.Acceleration);
         }
         if (isTouching && Input.GetKeyDown("space")) { //jump
             rigidBody.AddForce(new Vector3(0, jumpPower, 0) * 50, ForceMode.Acceleration);
         }
     }
 
     private void OnCollisionEnter(Collision collision) {
         isTouching = true;
     }
     private void OnCollisionExit(Collision collision) {
         isTouching = false;
     }
 }
 
Answer by Lazdude17 · Apr 07, 2018 at 06:35 PM
You never checked for WASD input. You assigned moveX and moveZ but never used them.
  public class PlayerController : MonoBehaviour {
  
      public float moveSpeed = 12;
  
      Rigidbody rigidBody;
      float moveX;
      float moveZ;
  
      bool isTouching = false;
      public float jumpPower;
  
      public Transform _camera;
  
      void Start () {
          rigidBody = this.GetComponent<Rigidbody> ();
  
          if (rigidBody == null)
              Debug.LogError ("RigidBody could not be found.");
      }
  
      void Update () {
          public float moveX = Input.GetAxis ("Horizontal");
          public float moveZ = Input.GetAxis ("Vertical");
 
  
          public Vector3 moveVector = new Vector3(transform.position.x*moveX, 0f, (transform.position.z-camera.transform.position.z)*moveZ);
      }
  
      void FixedUpdate () {
 
          if (rigidBody != null) {
              rigidBody.AddForce (moveVector * moveSpeed, ForceMode.Acceleration);
          }
          if (isTouching && Input.GetKeyDown("space")) { //jump
              rigidBody.AddForce(new Vector3(0, jumpPower, 0) * 50, ForceMode.Acceleration);
          }
      }
  
      private void OnCollisionEnter(Collision collision) {
          isTouching = true;
      }
      private void OnCollisionExit(Collision collision) {
          isTouching = false;
      }
  }
  
Your answer
 
 
             Follow this Question
Related Questions
Rigidbody movement in direction of camera? 1 Answer
Camera problem 2 Answers
Trying to get the camera to orbit, but also steer the player when moving. 0 Answers
Player character becomes shaky when camera is moving 0 Answers
Top Down Camera? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                