Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 epicjosh · Oct 24, 2014 at 02:17 PM · playersceneenemytarget

Enemy targeting player in different scene

Hi . How do i make enemy from another scene to target the player. My scene is like this, the player will destroy all enemy and when all enemies are destroyed the portal will open and the player will be transported to scene 2 and my enemies in scene 2 is supposed to look at the player which is not because i deleted the camera and the player in scene2 and used DontDestroyOnLoad(gameObject) so that the player in scene 1 will not be destroyed when entering to scene 2. I tried searching but i didn't find any.

Here's my script to my enemy:

 var distance;
 var target : Transform;    
 var lookAtDistance = 45.0;
 var attackRange = 25.0;
 var moveSpeed = 3.0;
 var damping = 6.0;
 var bullitPrefab:Transform;
 var savedTime = 0;
 
 private var isItAttacking = false;
  
 function Update () 
     {
     if(transform.rotation.x!=0){
  transform.rotation.x=0;
  }if(transform.rotation.z!=0){
  transform.rotation.z=0;
  }
             distance = Vector3.Distance(target.position, transform.position);
             
  
     if(distance < lookAtDistance)
     {
         isItAttacking = false;
         
         lookAt ();
     }   
     
     if(distance > lookAtDistance)
     {
         
     }
     
     if(distance < attackRange)
     {
         attack ();
     }
     if(isItAttacking)
     {
 
     }
 }
  
  
 function lookAt ()
 {
             isAttacking = true;
         var rotate = Quaternion.LookRotation(target.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damping);
         
         var seconds : int = Time.time;
         var oddeven = (seconds % 2);
         
         if(oddeven)
         {
             
             Shoot(seconds);
         
       }
 }
  
 function attack ()
 {
     isItAttacking = true;
    moveDirection = transform.forward * moveSpeed;
      GetComponent(CharacterController).SimpleMove(moveDirection);
 }
 
 function Shoot(seconds)
 {
     if(seconds!=savedTime)
     {
         var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , 
         Quaternion.identity);
         
         bullit.gameObject.tag = "enemyProjectile";
         bullit.rigidbody.AddForce(transform.forward * 3000);
     
         savedTime = seconds;
     }
 }
 

Any help would be appreciate.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Oct 24, 2014 at 02:24 PM

In the enemy's Start, use the Find method to find the player - either through the player object's name, tag or script Type. Assign the player as the enemy's target.

So, if your main character object is named "Player", you do this:

 function Start() {
     target = Find("Player");
 }

That should fix your problem.

Comment
Add comment · Show 4 · 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 epicjosh · Oct 24, 2014 at 02:31 PM 0
Share

i got an error Assets/Script/Js/PA_DroneCrontrol.js(13,18): BCE0005: $$anonymous$$ identifier: 'Find'.

avatar image Anxo · Oct 24, 2014 at 02:50 PM 0
Share

Its a GameObject Static Function. GameObject.Find("Player");

avatar image epicjosh · Oct 24, 2014 at 02:59 PM 0
Share

yea i tried that too but i got a null reference error

avatar image epicjosh · Oct 25, 2014 at 05:45 AM 0
Share

i tried to change the var target to GameObject and change the code at start target = GameObject.FindWithTag("shooter"); but i got a null reference at line 19. Everytime i enter scene 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

enemy targeting player in different scene 1 Answer

How can I load a scene when the enemy hits the player? 5 Answers

Enemy spawn then select target? 1 Answer

Enemy does not look the player 2D platformer. 1 Answer

Kill the Player? 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