- Home /
Rotate 3rd person camera around player
How to rotate camera around player freely with joystick/ controller.
-Camera should collide with objects in scene. -If camera faces player's front part of the body, The player should pull left stick down to keep running forward. And so on.
So when you push right stick to the right, camera moves around player to the right side(orbiting).
When you push it to the left, camera orbits around the player to the left.
When you push stick up, camera zooms in (to specific length).
When you pull stick down towards yourself, camera zooms out (to specific length).
I was thinking maybe putting a sphere collider in which camera could move always facing the player. That could possibly make camera collide with objects in scene. But now, how to freely move the camera?
This is my script now: Camera:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
public float RotateSpeed = 150,
MoveSpeed = 50;
float DeltaTime;
void Update()
{
DeltaTime = Time.deltaTime;
transform.Rotate(0, Input.GetAxis("LeftX") * RotateSpeed * DeltaTime, 0);
transform.Translate(0, 0, -Input.GetAxis("LeftY") * MoveSpeed * DeltaTime);
}
}
public class CameraScript : MonoBehaviour
{
public GameObject Target;
public float RotateSpeed = 170,
FollowDistance = 20,
FollowHeight = 10;
float RotateSpeedPerTime,
DesiredRotationAngle,
DesiredHeight,
CurrentRotationAngle,
CurrentHeight,
Yaw,
Pitch;
Quaternion CurrentRotation;
void LateUpdate()
{
RotateSpeedPerTime = RotateSpeed * Time.deltaTime;
DesiredRotationAngle = Target.transform.eulerAngles.y;
DesiredHeight = Target.transform.position.y + FollowHeight;
CurrentRotationAngle = transform.eulerAngles.y;
CurrentHeight = transform.position.y;
CurrentRotationAngle = Mathf.LerpAngle(CurrentRotationAngle, DesiredRotationAngle, 0);
CurrentHeight = Mathf.Lerp(CurrentHeight, DesiredHeight, 0);
CurrentRotation = Quaternion.Euler(0, CurrentRotationAngle, 0);
transform.position = Target.transform.position;
transform.position -= CurrentRotation * Vector3.forward * FollowDistance;
transform.position = new Vector3(transform.position.x, CurrentHeight, transform.position.z);
Yaw = Input.GetAxis("Right Horizontal") * RotateSpeedPerTime;
Pitch = Input.GetAxis("Right Vertical") * RotateSpeedPerTime;
transform.Translate(new Vector3(Yaw, -Pitch, 0));
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
transform.LookAt(Target.transform);
}
}
Movement:
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
well, it's easy to rotate the camera around the player. to move the player in regards to the camera position, you can find the vector between the camera and the player, this vector will give you up and down movements, "normalized" vector will give right and left movement. so it's not very hard to figure out after that. camera to collide will not be very easy, you have to decide on a method. you can put colliders around the camera, and use rays at the same time. if the camera collides, say a wall, than the ray finds the distance between the wall and the player and either lifts up the camera or gets it near the player. maybe someone will give a better advice than me.
Answer by nazia08 · Apr 07, 2014 at 10:54 AM
I know it's one of the commands that start with "thirdperson_", I don't know which one, though.