• 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 Fox-Handler · Jun 27, 2014 at 06:40 AM · targetinstantiation

Target lost once player is instantiated

I have a character that is reinstantiated whenever he gets attacked by turrets, but once he dies and I respawn him, the turret loses its target and they just stay motionless. How could I set the character as a target so that once he dies and is respawned, the turrets are still able to track him? About the closest i've gotten is finding the gameObject once the character is destroyed/null, but I still get an error.

 using UnityEngine;
 using System.Collections;

 public class Turret : MonoBehaviour 
  {
 public TurretGun turretGun;

 private float distance;
 private Quaternion rotation;
 private float lookAtDistance = 10;
 private float reactionTime = 3;
 private Quaternion originalRotation;
 private float attackRange = 10;
 public Transform target;

 void Start()
 {
     target = GameObject.FindGameObjectWithTag("Player").transform;
 }

 void Update ()                    
 {
     if(target)
     {    distance = Vector3.Distance(target.position, transform.position); 
         
         if (distance < lookAtDistance) 
         {
             lookAt();
         }
          if (distance > lookAtDistance) 
         {
             originalRotation = Quaternion.identity;
         }
         if (distance < attackRange)    
         {
             attack();
         }
     }
     else
     {
         target = null;
     }

     if (target == null)
     {
         Target ();
     }

 }

 public void Target()
 {
         target = GameObject.FindGameObjectWithTag("Player").transform;
 }
 
 void lookAt ()  
 {        
     
     rotation = Quaternion.LookRotation(target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * reactionTime);                                                                                
 }

 void attack()
 {
     turretGun.ShootContinuous();
 }

}

Comment
Add comment · Show 3
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 HarshadK · Jun 27, 2014 at 06:44 AM 0
Share

Can we see your code where you instantiate your player to respawn him?

avatar image Fox-Handler · Jun 27, 2014 at 07:07 AM 0
Share

Here it is

 using UnityEngine;
 using System.Collections;
 
 public class GameCamera : $$anonymous$$onoBehaviour 
 {
     private Vector3 cameraTarget;
     private Transform target; 
     private Game$$anonymous$$anager manager;
     private Vector3 startPoint = new Vector3(0,0,-6);
     public GameGUI gui;
 
     void Start () 
     {
         target = GameObject.FindGameObjectWithTag("Player").transform; 
         manager = GetComponent<Game$$anonymous$$anager>();
     }
 
     public void SetTarget(Transform t) 
     {
         target = t; 
         cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z);
         transform.position = Vector3.Lerp(transform.position, cameraTarget, Time.deltaTime * 8);
     }
 
 
 
     void Update () 
     {
 
 
         if(target)
         {
         cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z); 
         transform.position = Vector3.Lerp(transform.position, cameraTarget, Time.deltaTime * 8);
         }
         else 
         {
             cameraTarget = Vector3.zero;
         }
 
         if(target == null) 
         {
             if(Input.GetButtonDown("Respawn"))
             {
                 manager.SpawnPlayer(startPoint);
                 gui.ResetHealth();
             }
         }
     }       
 }
avatar image Fox-Handler · Jun 27, 2014 at 07:13 AM 0
Share
 using UnityEngine; using System.Collections;
 
 public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
 public GameObject player;
 private GameObject currentPlayer;
 private GameCamera cam; 
 
 
 void Start () 
 {
     cam = GetComponent <GameCamera>(); 
 }
 
 public void SpawnPlayer (Vector3 startPoint)
 {   
     currentPlayer = Instantiate (player, startPoint, Quaternion.identity)as GameObject;
     cam.SetTarget (currentPlayer.transform);
 }
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fox-Handler · Jun 27, 2014 at 07:56 AM

Alright took a break and somehow figured it out. Basically just invoked a function from the script of the main cannon of the gun when I push the respawn button. Not sure why It didn't work before, but probably just got confused by something.

         if(target == null) 
         {
             if(Input.GetButtonDown("Respawn"))
             {
                 manager.SpawnPlayer(startPoint);
                 gui.ResetHealth();
                 turret.ResetTarget();
 
             }
         }

     public void ResetTarget()
     {
         target = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
Comment
Add comment · Show 2 · 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 HarshadK · Jun 27, 2014 at 09:44 AM 0
Share

Congrats dude!

It feels proud for solving your problem on your own. Isn't it? :-)

avatar image Fox-Handler · Jun 27, 2014 at 10:58 AM 0
Share

heck yea, its definitely getting less tedious

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The AI-Players do not see new install (Instantiate) object (the ball) after they set the first goal in gates and old object (the ball) was destroyed. 0 Answers

Set clone as target 0 Answers

How to move camera target smoothly? 1 Answer

OnTrigger does not work for multi targets 1 Answer

TwoBoneIKConstraint Target wont update from script 0 Answers


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