- Home /
Combining two camera modes
I've been experimenting with a vehicle-based game where you can drive and fire a turret. I'd like the camera to follow the vehicle while turning, but also have it orbit around the vehicle to aim the turret at the same time.
These two (simplified) scripts work fine independently but trying to combine them is giving me an headache.
My follow script:
float currentAngle = transform.eulerAngles.y;
float desiredAngle = target.transform.eulerAngles.y;
float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime);
var rotation = Quaternion.Euler(0, angle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
My mouse orbit script:
x += Input.GetAxis("Mouse X") * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * Time.deltaTime;
var rotation = Quaternion.Euler(y, x, 0f);
var position = rotation * target.position;
transform.rotation = rotation;
transform.position = position + new Vector3(0f, 2.4f, 0f);
The one part I forgot to explain is that I want to have the camera rotate when the vehicle turns as well. So that it rotates when turning the car and when turning the turret.
Thank you for the effort though
oh that's even easier then, make the turret a child of the vehicle, and then attach the mouse look script from the character controller standard assets package to the turret object!
Answer by Scribe · Nov 18, 2014 at 05:17 PM
Here's a script I have used previously, maybe it would be useful to you!
Note that the code currently in FixedUpdate was just for testing purposes to see if this was the correct code, it is not advisable to set the velocity of a rigidbody directly and there are better choices for controlling rotation than is used here as well!
public GameObject target;
Rigidbody targetRB;
Transform targetTrans;
Transform thisTrans;
public Vector3 orbitOffset = new Vector3(0, 2, 0);
float viewAngle = 0;
float rot = 0;
public float rotationSpeed = 360;
public float zoomSpeed = 10;
float distance;
public float minDist = 5;
public float maxDist = 10;
public float maxSpeed = 10;
Vector3 lookPos;
Quaternion origRot;
void Start(){
targetRB = target.rigidbody;
targetTrans = target.transform;
distance = Mathf.Clamp(distance, minDist, maxDist);
thisTrans = transform;
lookPos = targetTrans.position + orbitOffset;
origRot = thisTrans.rotation;
}
void Update(){
lookPos = targetTrans.position + orbitOffset;
if(targetRB != null){
distance = Mathf.Lerp(minDist, maxDist, targetRB.velocity.sqrMagnitude/(maxSpeed*maxSpeed));
distance = Mathf.Clamp(distance, minDist, maxDist);
}
viewAngle += Input.GetAxis("Mouse Y")*rotationSpeed*Time.deltaTime;
rot += Input.GetAxis("Mouse X")*rotationSpeed*Time.deltaTime;
viewAngle = Mathf.Clamp(viewAngle, -80, 10);
Quaternion xQuaternion = Quaternion.AngleAxis (rot, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (viewAngle, Vector3.left);
thisTrans.rotation = origRot * xQuaternion * yQuaternion;
thisTrans.position = lookPos - (thisTrans.forward*distance*Mathf.Lerp(0.5f, 1, (10-viewAngle)/90));
}
void FixedUpdate(){
//REPLACE THIS WITH BETTER MOVEMENT CODE
if(targetRB == null){
return;
}
Vector3 v = targetRB.velocity;
v = Input.GetAxis("Vertical")*maxSpeed*targetTrans.forward;
targetRB.velocity = v;
targetTrans.Rotate(Vector3.up, Input.GetAxis("Horizontal")*rotationSpeed/2*Time.deltaTime);
}
Scribe
Your answer
Follow this Question
Related Questions
Camera position not updating 1 Answer
Mouse Orbit + Move Object + Follow Problem 1 Answer
Super Monkey Ball type Camera? 2 Answers
Rotate according to the camera on Y and Z only! 2 Answers
How to make your character move? 7 Answers