Question by
AaronDuke · Sep 16, 2016 at 04:46 AM ·
camera follow
how to make camera follow instantiated object?
I have a character the mage in this case which the camera follows and when the mage gets close to the golem it mounts it, this instantiates a new game object with the mage on top of the golem now I cant get the camera to follow this new instantiated object.
public class FollowCamera : MonoBehaviour {
public GameObject golem;
public GameObject MountedGolem;
public GameObject target;
public Camera mainCamera;
public float interpVelocity;
public float minDistance;
public float followDistance;
public Vector3 offset;
Vector3 TargetPos;
// Use this for initialization
void Start () {
TargetPos = transform.position;
}
// Update is called once per frame
void Update () {
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 10f;
TargetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
transform.position = Vector3.Lerp( transform.position, TargetPos + offset, 0.50f);
}
else
{
target = MountedGolem;
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 10f;
TargetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
transform.position = Vector3.Lerp( transform.position, TargetPos + offset, 0.50f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Does anyone know the script for a smooth camera follow of the main game object? 8 Answers
How to choose a specific game object with a tag from multiple game objects with that tag? 0 Answers
Create a smooth camera without jittering/flickering in 2D 3 Answers
Help with setting up 2D auto scrolling camera using Cinemachine 1 Answer