Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wtfitsed · Apr 18, 2015 at 11:52 PM · camerainstantiateclone

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);
 
                 }
     
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Soos621 · Apr 19, 2015 at 12:03 AM 0
Share

are you assigning the public Gameobject target in the editor?

6 Replies

· Add your reply
  • Sort: 
avatar image
2

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."

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wtfitsed · Apr 19, 2015 at 02:56 PM 0
Share

Edit: I think my comment went on the wrong thread. I will try this.

avatar image
0

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
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Amoldadu · Mar 08, 2017 at 09:15 AM 0
Share

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....

avatar image
0

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 .

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NecrosDk · Mar 08, 2017 at 03:54 PM 0
Share

This was meant for @Amoldadu

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera follow to clone player not first production ? 0 Answers

Instantiate Object Problem 0 Answers

How do I stop Script from making multiple Cameras? 1 Answer

(solved)Instantiate prefab "on top" of previous one 1 Answer

how can i take reference to the clone and not to the Gameobject? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges