Hi friends, I'm trying to make my first 2D Running Game and I would like to know how to make a fixed Following Camera (EX: The Player position to be always on the bottom left) I made a graphic example I have this script to fixing camera on player: Code (CSharp): using UnityEngine; using System.Collections; public class PlayerCam: MonoBehaviour { public Transform player; void Update () { transform.position = new Vector3 (player.position.x + 7, 5, -20); } } But I want the camera to follow the player on jumping moving on Y axises....any suggestion for doing this please
Look at the line of code in your Update method. It is positioning the camera horizontally in relation to the player's position. So... with that pattern in place and working... how do you think you could change the hard-coded value of 5 to achieve the tracking of the player's vertical position...
Problem solved ....the finnal code: Code (CSharp): using UnityEngine; using System.Collections; public class CameraJucator : MonoBehaviour { public int yOffset = 5; private Transform player; void Awake() { player = GameObject.FindGameObjectWithTag("Player").transform; } void Update () { transform.position = new Vector3(player.position.x + 7, player.position.y + yOffset, -30); } }
@D0R1N that is pretty close to what I use. I would make the script a little more robust and allow the script to track other objects by passing in the reference to what you want the camera to track Code (CSharp): public GameObject trackableObject; and then maybe just using a vector 3 for all offsets Code (CSharp): public Vector3 offsetPositions; Finally in your update for the camera you transform would be something like. Code (CSharp): void Update() { // Find our object's position Vector3 trackablePosition = trackableObject.position; // add offsets trackablePosition.x += offsetPositions.x; trackablePosition.y += offsetPositions.y; trackablePosition.z += offsetPositions.z; // Let our camera use it transform.position = trackablePosition; } This way you can just continue to reuse the code over and over again.
Just to add something, if you want to add height or distance on the "X" axis, add values to the position.x or position.y with all this my camera works excellent
Hi guys, I wonder how to make the camera not following the player on Y axis only if the player make the second jump or get's a specific altitude on Y, any ideas how to achieve this ?!