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 BlackWingsCorp · Jan 14, 2014 at 06:05 PM · c#rotationenemyfreezeenemyai

Script freezing Unity

Hey guys I wrote an AI script that was supposed to make the enemy rotate towards the player once he enters it's trigger collider but everytime I run it unity freezes. Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
 
     public float speed = 2.0f;
     public GameObject target;
     public float dist;
     // Use this for initialization
     void Start () {
         GameObject.FindGameObjectWithTag("Player");
     
     }
     
     // Update is called once per frame
     void Update () {
         dist = Vector3.Distance(transform.position, target.transform.position);
     }
 
     void OnTriggerEnter(Collider hit){
         while(hit.gameObject.tag == "Player"){
             transform.rotation = Quaternion.Lerp(transform.rotation, target.transform.rotation, Time.deltaTime * speed);
             transform.Translate(Vector3.forward * speed * Time.deltaTime);
         }
     }
 }


PS: I tried "if" instead of "while" but in only worked on rotating the enemy one frame

Comment
Add comment · Show 2
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 woodoo · Jan 14, 2014 at 06:12 PM 1
Share

$$anonymous$$akes sense. It will freeze your game, or at least this GameObject.

void OnTriggerEnter(Collider hit) receives the Collider, but it won't change inside the function, it is a local instance, so the tag will always be "Player", you'll never leave this loop without telling it to leave.

avatar image BlackWingsCorp · Jan 14, 2014 at 06:19 PM 0
Share

Yeah I just switched it back to if. If it's not much trouble do you know how to make the enemy look at the player every frame as long as the player's within the trigger area?

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AndyMartin458 · Jan 14, 2014 at 06:09 PM

Oh I see the problem, you need to use the function OnTriggerStay with an if.

 void OnTriggerStay(Collider hit)
 {
     if(hit.gameObject.tag == "Player")
     {
         transform.rotation = Quaternion.Lerp(transform.rotation, target.transform.rotation, Time.deltaTime * speed);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
 }

In Start(), you should probably recode to "target = GameObject.FindGameObjectWithTag("Player");" or remove your start function altogether. You are probably getting a null reference exception because target is null.

 void Start () 
 {
     target = GameObject.FindGameObjectWithTag("Player");
 }
Comment
Add comment · Show 7 · 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 BlackWingsCorp · Jan 14, 2014 at 06:17 PM 0
Share

Thanks, yeah I had to drag the player character manually to the target variable in the hierarchy.

avatar image AndyMartin458 · Jan 14, 2014 at 06:19 PM 0
Share

@BlackWingsCorp I modified my answer to include OnTriggerStay

avatar image BlackWingsCorp · Jan 14, 2014 at 06:37 PM 0
Share

Thanks @Andy$$anonymous$$artin458 it worked! Do you know how to make the enemy rotate to the position of the player rather than his rotation? It would be greatly appreciated

avatar image deltamish · Jan 14, 2014 at 06:47 PM 0
Share

I am sorry i couldn't understand this

enemy rotate to the position of the player rather than his rotation

please explain a bit more

avatar image BlackWingsCorp · Jan 14, 2014 at 06:51 PM 0
Share

in my script the enemy looks towards the rotation of the player in other words wherever the player is looking at the enemy will look at to ins$$anonymous$$d of looking at the player's position

Show more comments
avatar image
1

Answer by JoaoOliveira · Jan 14, 2014 at 06:13 PM

The condition you are testing never changes inside the while loop, therefore it is an infinite loop.

What you might want is a field that is changed according to player detection:

 private bool _collidingWithPlayer;
 
 void OnTriggerEnter(Collider hit){
        if(hit.gameObject.tag == "Player"){
          _collidingWithPlayer = true;
        }
 }
 
 void OnTriggerExit(Collider hit){
        if(hit.gameObject.tag == "Player"){
          _collidingWithPlayer = false;
        }
 }
 
 void Update () {
        dist = Vector3.Distance(transform.position, target.transform.position);
        if(_collidingWithPlayer){
          transform.rotation = Quaternion.Lerp(transform.rotation, target.transform.rotation, Time.deltaTime * speed);
          transform.Translate(Vector3.forward * speed * Time.deltaTime);
        }
 }
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 BlackWingsCorp · Jan 14, 2014 at 06:39 PM 0
Share

Simple and efficient. Thanks! Any idea on how to make the enemy rotate towards the player's position rather than rotation? I tried to use the transform.LookAt() but didn't work

avatar image
1

Answer by woodoo · Jan 14, 2014 at 06:21 PM

You could modify your loop to something like this and probably wou'll get what you want:

 void onTriggerEnter(Collider hit)
 {
     while (true)
     {
         /* your code here */
 
         /* here you create some collision verification, like OverlapSphere, and if it doesn't collide, you just break and leave the loop */
     }
 }
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

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

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

Related Questions

Enemy fire does not propel in any direction? 1 Answer

Slerp Problem?: Enemy in Constant Rotation! 1 Answer

How to freeze enemy movement when Hurt/enemy Damage? 1 Answer

Freeze specific rotation axis of a child 2 Answers

Moving character from point to point 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