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 Shinxs · Oct 06, 2012 at 02:01 PM · updateontriggerenterhow to

How to call a OnTriggerEnter function in a Update function?

How can I get (In Javascript) a OnTriggerEnter function in a Update function for a Enemy AI. This is the code: var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player").transform; }

function Update(){

transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));

myTransform.Translate(Vector3.forward moveSpeed Time.deltaTime);

}

BTW if you know a way to make this code smaller please say it.

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

2 Replies

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

Answer by m4s4m0r1 · Oct 06, 2012 at 02:12 PM

Impossible... Because OnTriggerEnter and Update is a different function. So, the function cannot be called in another function. If you want to do that you can make a variable to solve that For Example : if you want to call that Trigger was true. here it this

     var yourVar = false;
     
     function OnTriggerEnter(other : Collider)
     {
     yourVar = true;
     }
     
     function Update()
     {
     if(yourVar)//if yourVar is true
     {
     //do something in here
     }
     }
Comment
Add comment · Show 5 · 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 Shinxs · Oct 06, 2012 at 05:03 PM 0
Share

Thank You it worked it's not hard ;)

avatar image m4s4m0r1 · Oct 06, 2012 at 11:08 PM 0
Share

please, mark it as true answers

avatar image darrenxyler · Oct 26, 2014 at 08:17 AM 0
Share

how to write this in c#? i am an absolute beginner when it comes to codes...

avatar image Kiwasi · Oct 26, 2014 at 08:35 AM 0
Share

You probably shouldn't, but here it is:

 public class $$anonymous$$yClass {
     bool yourVar;
 
     void OnTriggerEnter (Collider other){
         yourVar = true;
     }
 
     void Update () {
         if(yourVar){
             // Do something
         }
     }
 }


avatar image darrenxyler · Oct 26, 2014 at 08:52 AM 0
Share

Thanks a lot... so it is actually BOOL ins$$anonymous$$d of var...

avatar image
0

Answer by Dragonlance · Oct 06, 2012 at 02:54 PM

Shinxs you need to think in form of events.

Sure you can move the rocket or whatever in Update and if that move resulted in a collision with a trigger, then OnTriggerEnter will be called and you explode it there.

You do not need to check if a collision happened in Update. You can of course do it i.e. with RigidBody SweepTest() SweepTestAll() but you do not have to.

Anyway for such stuff to work both game objects need to have a rigidbody and collider attatched and one has to be flagged with IsTrigger if you want to use OnTriggerEnter.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Calling OnTriggerEnter every update if something is false 0 Answers

OnTriggerEnter not working 5 Answers

OnTriggerEnter -- NullReferenceException -- C# 2 Answers

Why is my update function acting wierd? 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