- Home /
Adapting Velocity-based Platformer Controls to Camera Forward
I'm making a 3D platform game, and I've gotten the basic control scheme that I want written and working except for one thing: Camera-relative movement.
See, I have the control scheme working perfectly with world-based axes. It adds to the player's Rigidbody velocity and has the player face the velocity. I also have a camera system where the camera is a child of a GameObject following the player that can be rotated left and right (also up and down, but this is irrelevant). I would like for the player to accelerate forwards and sideways based on the camera follower's forward and right. However, that has proven to be the toughest challenge for me.
I won't post the entire code file's contents, because it also has code unrelated to ground movement, so I'm pasting the current state of the movement code that uses Vector3 directions:
var speed : float = 3;
var topSpeed : float = 10;
function FixedUpdate () {
if (canMove){
GetComponent.<Rigidbody>().velocity += (Vector3.right.normalized * speed) * Input.GetAxisRaw("Horizontal");
GetComponent.<Rigidbody>().velocity += (Vector3.forward.normalized * speed) * Input.GetAxisRaw("Vertical");
if (Input.GetAxisRaw("Horizontal") != 0){
GetComponent.<Rigidbody>().velocity.x = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.x,(-1 * (topSpeed * Mathf.Abs(Input.GetAxisRaw("Horizontal")))),(topSpeed * Mathf.Abs(Input.GetAxisRaw("Horizontal"))));
}
if (Input.GetAxisRaw("Vertical") != 0){
GetComponent.<Rigidbody>().velocity.z = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.z,(-1 * (topSpeed * Mathf.Abs(Input.GetAxisRaw("Vertical")))),(topSpeed * Mathf.Abs(Input.GetAxisRaw("Vertical"))));
}
}
}
The camera follower has a basic script that rotates it along the Y axis, so I won't post that. However, I'd like for the movement to be in the direction of the camera follower. Every attempt I've tried so far has failed, including using Quaternion.LookDirection, and while the closest attempt allowed for diagonal movement, it caused the acceleration to go through the roof as well as causing a "Sonic 4 effect" where the character would stop on a dime in mid-air.
I don't want to use transform.Translate because that will ignore physics. AddForce doesn't work for some reason, and I need to add the velocity because I don't want to mess up the Y velocity.
Is something like this going to work with the camera rotation, or does it all need rewritten to use something besides velocity?
Your answer
