- Home /
camera follow with distance
hello everyone, i got a 2d infinite runner and using this script for follow to player: using UnityEngine; using System.Collections;
public class CameraRunnerScript : MonoBehaviour {
public GameObject cameraTarget;
public GameObject player;
public float xMargin = 1f;
public float yMargin = 1f;
public float smoothTime = 0.1f;
Vector2 velocity;
private Transform thisTransform;
void Start()
{
thisTransform = transform;
player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
if (CheckXMargin())
{
thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
}
if (CheckYMargin())
{
thisTransform.position = new Vector3(thisTransform.position.x, Mathf.SmoothDamp(thisTransform.position.y, cameraTarget.transform.position.y, ref velocity.y, smoothTime), thisTransform.position.z);
}
}
bool CheckXMargin()
{
return Mathf.Abs(thisTransform.position.x - player.transform.position.x) > xMargin;
}
bool CheckYMargin()
{
return Mathf.Abs(transform.position.y - player.transform.position.y) > yMargin;
}
}
this is a very good solution for following with margin, but now player is center on screen. and its creating a less visible area for player. so how can i change this script for camera following to player with some x asis distance and player position always close to left side.
Answer by Maerig · May 26, 2014 at 12:37 AM
Place the cameraTarget
GameObject ahead of the player ?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I only want my character to jump when touching the ground 4 Answers
Choppy camera follow. 1 Answer
C# 2D InfiniteRunner run with same speed even when going up 1 Answer
Player enable 1 Answer