- Home /
 
 
               Question by 
               SylvaxNova04 · Jan 24 at 02:52 AM · 
                camerarigidbodycamera-movementcontrolsboxcollider  
              
 
              Moving forward relative to camera with atypical WASD script, cinemachine, rigidbody and boxcollider
Hello, I am using atypical WASD movement controls because they provide the best physics to my type of character so far (although I am open to a better method giving the same results). Worth nothing my character is a cube, using a rigidbody and a box collider that can tumble in all directions. I would like for that character to be able to move with WASD keys and for the forward motion to be relative to my camera, which can rotate relative to my cursor. I'm currently using cinemachine ''freelook'' but experimented with ''virtual - 3rd person follow''. I've searched the entire forum pages and nothing made it so far. Here's my character code:
 public class TINYcontrols : MonoBehaviour
 {
     //VARIABLES
     [SerializeField] private Vector3 v3FrontBack;
     [SerializeField] private Vector3 v3RightLeft;
     [SerializeField] private float maxSpeed;
 
     [SerializeField] private bool isGrounded;
     [SerializeField] private float groundCheckDistance;
     [SerializeField] private LayerMask groundMask;
     [SerializeField] private float jumpForce;
 
     [SerializeField] private KeyCode keyW;
     [SerializeField] private KeyCode keyS;
     [SerializeField] private KeyCode keyD;
     [SerializeField] private KeyCode keyA;
 
     private Rigidbody rb;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     private void Update()
     {
         Move();
 
         Jump();
     }
 
     private void Move()
     {
         //GroundCheck
         isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
 
         // WASD Controls
         if (Input.GetKey(keyW))
             rb.velocity += v3FrontBack;
 
         if (Input.GetKey(keyS))
             rb.velocity -= v3FrontBack;
 
         if (Input.GetKey(keyD))
             rb.velocity += v3RightLeft;
 
         if (Input.GetKey(keyA))
             rb.velocity -= v3RightLeft;
 
         // MaxSpeed
         float speed = Vector3.Magnitude(rb.velocity);  // test current object speed
 
         if (speed > maxSpeed)
         {
             float brakeSpeed = speed - maxSpeed;  // calculate the speed decrease
 
             Vector3 normalisedVelocity = rb.velocity.normalized;
             Vector3 brakeVelocity = normalisedVelocity * brakeSpeed;  // make the brake Vector3 value
 
             rb.AddForce(-brakeVelocity);  // apply opposing brake force
         }
     }
 
     private void Jump()
     {
         if (isGrounded)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
             }
         }
     }
 }
 
               Yes I am a complete beginner, thanks for any tips :)
               Comment
              
 
               
              Your answer