- Home /
Moving 3rd person player relative to orbiting camera
Hello all, I am working on a prototype for a 3D platformer. :) I have set up some scripts, but I have encountered a big problem. If you ever played Mario 64 or any of the 3D Zeldas, you will know that they work in a strange way. When you push your joystick up or down, the player moves away from or towards the camera, and when you push your joystick left or right, he actually orbits around the camera, because the camera changes angle to face him and he continues to move to the camera's right or left. It is also important to note that the directions can be mixed, so if you push to the upper right, he actually spirals outwards away from the camera, as he moves both away from the camera and around it. My player is currently a simple cube with a character controller and this script, which is designed to work for a controller although it also works with a keyboard. He moves around using wasd or a joystick, but he moves relative to the world axis, not the camera axis. Obviously this causes problems, because if the camera is rotated through 180 degrees, all the controls are inversed!
var horizontalSpeed = 1.0f;
var verticalSpeed = 1.0f;
var rotationSpeed = 60.0f;
var jumpSpeed = 8.0f;
var gravity = 14.0f;
var cameraTransform : Transform;
var axis : Vector3 = Vector3.up;
private var moveDirection = Vector3.zero;
private var controller : CharacterController;
function Update()
{
controller = gameObject.GetComponent(CharacterController);
if (controller.isGrounded)
{
var horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed;
var forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
else
{
horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed / 2;
forwardDirection = Input.GetAxis("Vertical") * verticalSpeed / 2;
moveDirection = new Vector3(horizontalDirection, moveDirection.y, forwardDirection);
moveDirection.y -= gravity * Time.deltaTime;
}
if (moveDirection.magnitude > 0.05)
{
transform.LookAt(transform.position + new Vector3(moveDirection.x, 0, moveDirection.z));
}
controller.Move(moveDirection * Time.deltaTime);
}
Then there's the camera, which has 2 scripts.
The standard one to follow the player's movement:
var target : Transform;
var rotationDamping = 3.0;
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
if (!target)
return;
var wantedRotationAngle = target.eulerAngles.y;
var currentRotationAngle = transform.eulerAngles.y;
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
transform.LookAt (target);
}
And another one, which keeps it the right distance from the player and orbits around him:
var setHeight : float;
var maxDistance : float;
var minDistance : float;
var target : Transform;
var axis : Vector3 = Vector3.up;
var rotationSpeed = 100;
function Update ()
{
var distance = Vector3.Distance(this.transform.position, target.transform.position);
if(distance > maxDistance)
{
Debug.Log("Too far away");
//Vector3.MoveTowards(transform.position, target.transform.position, 100);
//transform.position += Vector3.forward * 4 * Time.deltaTime;
transform.Translate(Vector3.forward * 10 * Time.deltaTime);
}
else if(distance < minDistance)
{
Debug.Log("Too close");
transform.Translate(Vector3.back * 10 * Time.deltaTime);
}
transform.position.y = target.position.y + setHeight;
if(Input.GetAxis("RightHorizontal") < 0)
{
transform.RotateAround (target.position, axis, rotationSpeed * Time.deltaTime);
}
else if(Input.GetAxis("RightHorizontal") > 0)
{
transform.RotateAround (target.position, axis, -rotationSpeed * Time.deltaTime);
}
}
I would very much appreciate any help with this, either with the existing code or new code entirely. It needn't be particularly efficient, as this is just a prototype, and C# alternatives are also fine! :) Thanks! :D
Answer by yanuaris · Feb 29, 2016 at 12:06 PM
I'm still solving this too myself, and i'm not near any unity to proof this. Assuming you've made your variable to contain the camera transform...
moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);
We can start from here, where you should probably let moveDirection acquire Vector(cameratransform.right,0, cameratransform.forward);
This one can also be your solution, calling LookAt first to make the character point towards that specific direction from the camera angle.
if (moveDirection.magnitude > 0.05)
{
transform.LookAt(transform.position + new Vector3(0, 0, cameratransform.forward));
}
While this one feels more direct, applied to your character.
transform.LookAt(cameratransform.forward);
I'm nowhere close to being sure and seeing how you type so much i think you'll understand more if you test it. Let me know about the result and maybe once i got back from work i'll try your code and see if i can solve it.
@yanuaris That's very interesting and surprisingly simple... I'll try this out when I next get chance. I haven't done any program$$anonymous$$g for a while, but hopefully we'll work this out eventually! Thanks