- Home /
 
(2d in 3d) Movement relative to camera using JS
Hello and thanks for reading.
I am in the process of learning javascript and have programmed a working movement script using a rigid body as seen below. In this project, I'm trying to create a prototype that allows a 2D character to exist within a 3D (platform) style world. In order to do so, the player will move the avatar using WASD and press "q" (-90 rotation) and "e" (+90 rotation) for camera rotation. The problem I am encountering is that once I keybind "q" or "e" to rotate the camera, the parent object (the character) still uses world X and Z axis.
How do I go about rotating the camera in a fixed -+90 degree position, yet allowing relative movement using WASD? Any help would be appreciated. This is a learning purpose, so I would appreciate it even more if the answer(s) are explained beyond just tossing out a function call.
Thanks!!
 #pragma strict
 
 var speed : float = 10.0;
 var jumpSpeed : float = 200.0;
 var grounded : boolean = true;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
 
 // Allows X-Axis (left and right) and Z-Axis (to and from camera) movement times movement speed.
     var x : Vector3 = Input.GetAxis("Horizontal") * transform.right * Time.deltaTime * speed;
     var z : Vector3 = Input.GetAxis("Vertical") * transform.forward * Time.deltaTime * speed;
     transform.Translate(x + z);
     transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up);
     
 // Allows jumping if spacebar is held down on keyboard.    
     if(Input.GetButtonDown("Jump"))
     {
         Jump();
     }
 }
 
 // If grounded, allows jump.  If character is not grounded, the player cannot jump.
 function Jump ()
 {
     if(grounded == true)
     {
     rigidbody.AddForce(Vector3.up * jumpSpeed);
     grounded = false;
     }
 }
 
 function OnCollisionEnter(hit : Collision)
 {
     grounded = true;
 
 }
 
              Your answer
 
             Follow this Question
Related Questions
How to make my player look in the direction of its movement? 3 Answers
RTS Camera Rotation and Movement 0 Answers
camera slides and bounces while moving C# 1 Answer
Fixed camera on rails. 2 Answers
Global movement 0 Answers