Hello, I am trying to get my main camera to follow my main sprite but only on the x-axis. I have it as a child of the main sprite but it follows it everywhere. Any help is appreciated.
Slap a script on the camera telling it to update it’s position based on the players position.
And make sure the camera isn’t a child of anything. That generally isn’t a good idea.
Something like this should do, this should track the player on the X axis and should give it some basic smooth movement (use an interpSpeed between 1 and 0, 1 move instantly, 0 never moves);
Public GameObject player;
Public float interpSpeed;
Private Vector3 targetPos
Void Update(){
targetPos = new Vector3(player.transform.position.x, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos, interpSpeed);
}
Prototypetheta gave a wonderful response! He’s pretty much spot on, but I would avoid using the Update() method for camera related things. Instead I would use LateUpdate().
Whoops, forgot about LateUpdate.
Do what he says, do camera stuff in LateUpdate.
Thanks! Will having my other code in js matter? I’d like to keep it all the same and I don’t want to copy the C# code and not know what it means.
It’s always best to keep your code consistent, so I wouldn’t recommend mixing C#/JS. You can do it, but it can become a nightmare. You should be able to convert this simple script into JS. If you need any help, hit me up ![]()