- Home /
I ended up just increasing the 2D Sky Texture as it was too much trouble for just a little thing.
How to make Main Camera to not follow object on Y
Hi.So i have this 2D game where my character can move and jump and I want my camera to follow him on the X position but it also follows him on the Y.I have stoped this by changing the camera Y position every frame.This does not really help because when the character jumps the camera strarts to vibrate heavly. The script is from the sample assets:
using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
// Use this for initialization
void Start ()
{
lastTargetPosition = target.position;
offsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
void Update ()
{
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x,3.898071f,this.gameObject.transform.position.z);
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - lastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget) {
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
} else {
lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
lastTargetPosition = target.position;
}
}
Making the Y position change in the Fixed Update causes even more vibrations;
$$anonymous$$y code for only moving on x is :
using UnityEngine;
public class FollowCam : $$anonymous$$onoBehaviour {
public GameObject Player;
public GameObject Camera;
private float playerPosX;
void Update()
{
playerPosX = Player.transform.position.x;
Camera.transform.position = new Vector3(playerPosX, Camera.transform.position.y, Camera.transform.position.z);
}
}
Answer by VIPINSIRWANI · Aug 11, 2014 at 07:29 AM
You Can try Like that it will work for you in my case its following boy for only Z Direction.
public GameObject boy;
public Transform Cam;
void Update () {
boy.transform.Translate(0,0,0.4f);
Cam.transform.position = new Vector3(Cam.transform.position.x,Cam.transform.position.y,boy.transform.position.z - 5);
}
this make the boy go at high speed in one direction and falling through the map
Answer by khaled235711 · Aug 11, 2014 at 09:32 AM
If you Just Need a X-Follow Script here is one put this script to the camera
using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour
{
public Transform Player;
void Update()
{
transform.position = new Vector3(Player.position.x, transform.position.y, transform.position.z);
}
}
Answer by Subhajit-Nath · Aug 11, 2014 at 07:26 AM
Just store the initial Y position of the camera in the Start () method and use it in the follow vecotr's Y position.