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 IEMatrixGuy · Jul 10, 2014 at 05:20 AM · c#triggerai

Weeping Angel AI / OnTriggerStay Problems

Hi,

I have a C# script with which i am trying to create a simple weeping angel style AI. I am using OnTriggerStay to check wether or not he is in his area (defined by a trigger named WeepingQuadrant). However I am having trouble keeping him in his quadrant while the player is not in the quadrant, as currently he quickly teleports in and out of the quadrant. However he is behind the player long enough for the damage script to damage the player, even though he is not supposed to be doing so.

Below is my code:


 using UnityEngine;
 using System.Collections;
 
 public class WeepingAI : MonoBehaviour
 {
 
         public Transform player;
         MeshRenderer objectRenderer;
         Vector3 originalPos;
         public bool inTrigger = false;
         public float dotProduct;
         bool canHurt = false;
         
         // Use this for initialization
         void Start ()
         {
                 originalPos = transform.position;
                 objectRenderer = GetComponentInChildren<MeshRenderer> ();
         }
     
     
         void FixedUpdate ()
         {
                 if (!inTrigger) {
                         transform.position = originalPos;
                         canHurt = false;
                         gameObject.GetComponent<MeshRenderer> ().enabled = false;
                 }
                 if (inTrigger) {
                         if (CheckIfOffScreen ()) {
                                 //All move code here;
                                 transform.LookAt (new Vector3 (player.position.x, transform.position.y, player.position.z));
                                 if (Vector3.Distance (player.position, transform.position) > 5) {
                                         transform.position = player.position - player.forward;
                                 }
                                 if (Vector3.Distance (player.position, transform.position) < 1) {
                                         player.GetComponent<Player> ().health -= 0.01f;
                                         player.GetComponent<Player> ().HurtSound ();
                                 }
                         }
                 }
         }
         
         
         //If hes in his quadrant
         IEnumerator OnTriggerStay (Collider col)
         {
                 yield return new WaitForFixedUpdate ();
                 if (col.CompareTag ("WeepingQuadrant")) {
                         //Check if the player can see it
                         inTrigger = true;
 
                 }
         }
         void OnTriggerExit (Collider col)
         {
                 if (col.CompareTag ("WeepingQuadrant"))
                         inTrigger = false;
 
         }
         
         bool CheckIfOffScreen ()
         {
                 Vector3 playerFwd = player.forward;
                 Vector3 targetDir = (player.position - transform.position).normalized;
                 dotProduct = Vector3.Dot (playerFwd, targetDir); 
                 if (dotProduct < -0.5) {
                         return false;
                 } else {
                         return true;
                 }
                 
         }
 }



Any help would be greatly appreciated.

Thanks,

IEMatrixGuy out.

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 Kiwasi · Jul 10, 2014 at 05:58 AM 0
Share

Have you tried OnTriggerEnter ins$$anonymous$$d?

Another alternative would be to change in trigger to false at the end of every update cycle

avatar image IEMatrixGuy · Jul 10, 2014 at 05:59 AM 0
Share

the same thing happens :(

avatar image IEMatrixGuy · Jul 10, 2014 at 06:01 AM 0
Share

if i set inTrigger to false at the end of each update, the enemy slowly sinks through the ground :/ and doesnt chase you at all (with the OnTriggerEnter)

And with OnTriggerStay and the update, the same problem as before occurs.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unimechanic · Jul 11, 2014 at 03:01 PM

If you want physics to work correctly manipulate objects with Rigidbody's members and do it in FixedUpdate. I see you are using Transform.

Physics Tips:

If a collider is loaded/unloaded along with the scene and is not altered in any way, it can be a static collider (without Rigidbody).

If a collider is created/destroyed dynamically or is modified in some way, it must be a dynamic collider (needs a Rigidbody).

Altering means enabling (SetActive) or transforming (moving, rotating, scaling).

Whenever there is a GameObject with a Rigidbody, it must always be transformed with the corresponding Rigidbody's members, always in FixedUpdate.

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 IEMatrixGuy · Jul 12, 2014 at 12:20 AM 0
Share

I am using fixed update though, and the thing is returning to its position, its just got just enough time behind the player to do damage. Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer

Multiple Cars not working 1 Answer

Problems when i have multiple enemies on the same scene C# 1 Answer

Help with AI follow range 0 Answers

Emit Particle upon Death 2 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