Hi, I’m in progress in making a 2D racing game and I need a help. I’ve tried to put a camera that will follows the player’s car by placing it on my car as it’s child object. When I played it the camera works fine and follows the car but when it starts climbing and jumping a ramp the camera just rotating like mad. How I keeps the camera steady and makes it doesn’t rotating when my car rotating too…
btw, it’s my first thread. Sorry if I place this question on wrong place
The easiest way to do this is simply to make a script that locks the global rotation of the camera:
//put this on the camera
public class LockRotation : MonoBehaviour {
Quaternion startRotation;
void Awake() {
startRotation = transform.rotation;
}
void Update() {
transform.rotation = startRotation;
}
}[Code]
.rotation is the global rotation, so that'll prevent your camera from rolling with the car.
For later, you probably don't want to do the camera this way. Instead, you'll want a script on the camera that has it follow the car with code. That's more flexible, and you really need cameras to feel good.
An alternative is to get Cinemachine, which is a Unity-made free system for camera movement. I haven't tried it, but it looks good. [URL='https://www.assetstore.unity3d.com/en/#!/content/79898']Here's[/URL] the asset store link.
I’ve applied your code and it’s works fine. The camera isn’t rotating with the car again. But it starts shaking up and down when my car heading a ramp. I’ve tried to edit your code but I can’t fix that… I better check that cinemachine thing
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cam_script : MonoBehaviour {
Quaternion startRotation;
Vector2 startPostition;
void Awake () {
startRotation = transform.rotation;
startPostition = transform.position;
}
// Update is called once per frame
void Update () {
transform.rotation = startRotation;
transform.position = startPostition;
}