- Home /
How to follow clone object with camera.
I am trying to figure out how to make my camera follow a cloned prefab, but to no avail. Is there something I should add to the code to help it find cloned objects?
using UnityEngine;
using System.Collections;
using ORKFramework;
public class CameraFollow : MonoBehaviour {
public Transform target;
public float m_speed = 0.1f;
Camera mycam;
// Use this for initialization
void Start ()
{
mycam =GetComponent<Camera> ();
}
// Update is called once per frame
void Update () {
mycam.orthographicSize = (Screen.height / 2f) / 1f;
if (target) {
transform.position = Vector3.Lerp (transform.position, target.position, m_speed) + new Vector3 (0, 0, -10);
}
}
}
are you assigning the public Gameobject target in the editor?
Answer by RLin · Apr 19, 2015 at 02:55 PM
You can add a tag to the object, for example "targetobject." Then you can set the camera to search for objects with the tag "targetobject."
Edit: I think my comment went on the wrong thread. I will try this.
Answer by mycarm · Apr 19, 2015 at 02:57 PM
I use the code below to track target and then when I use the camera in my other script I use mycam.SetTarget (currentPlayer.transform);
// Track target
void LateUpdate() {
if (target) {
float x = IncrementTowards(transform.position.x, target.position.x, trackSpeed);
float y = IncrementTowards(transform.position.y, target.position.y, trackSpeed);
transform.position = new Vector3(x,y, transform.position.z);
}
}
// Increase n towards target by speed
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
}
}
Answer by Berge · Apr 19, 2015 at 03:18 PM
Not sure how or when you instantiate the clone, but you need to store a reference to the clone and not to the prefab. So when the new clone is instantiated you should save it in a variable "GameObject prefabClone". Then you can get the current position "prefabClone.transform.position" inside the Lerp method.
Please help me $$anonymous$$r. @Berge, I am having same problem regrading the following the camera to the clone of arrow. I am developing and 2d game same as bow and arrow game in which I want to attach camera follow script to an clone of arrow. but my previous script to works but it attached to the main arrow position. Once plays the game the camera came to the arrow spawning position and when i instantiate an arrow from dragging the bow string the clone of an arrow came and the arrow fires from bow but the camera does not follow clone of a Arrow, it follow the position from where the arrow fired. Or how I can store the Arrow(Clone) as object so I can drag it in my projectile script attached to the main camera. Thanks in Advance....
Answer by shahbazali63 · Mar 11, 2017 at 10:28 AM
void LateUpdate() { GameObject Clone = GameObject.instantiate ........; MyCameraRefernce.SetTarget (Clone); // if there is a function called SetTarget pass you gameobject there . or MyCameraRefernce.Target = Clone; // if its a varrible .
}
Answer by NecrosDk · Mar 08, 2017 at 03:53 PM
You can save a reference when you instantiate objects:
//Define this:
GameObject newArrow;
public GameObject arrowPrefab;
void Update(){
if (Input.GetButtonDown("FireArrow"){
newArrow = (GameObject) Instantiate(arrowPrefab, //Pos, //Rotation);
//Now you can reference the arrow you just spawned. For example:
Camera.main.GetComponent<FollowScript>().Follow(newArrow);
}
}
Your answer
Follow this Question
Related Questions
How to make camera follow to clone player not first production ? 0 Answers
Irregular position when instantiating clone during camera rotation? 1 Answer
Unity2D: How do I stop instantiate spawn (clone)? 3 Answers
Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers