Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by thePaintBrush · Apr 13, 2016 at 07:10 AM · unity 5javascriptrigidbodyaddforceontriggerenter

Having a OnTriggerEnter make another gameobject Add Force in UnityScript.

Using javascript, I was experimenting with OnTrigger functions with a trigger game object, I thought about triggering another gameobject to add force. Problem is, I don't understand how it would work with a OnTriggerEnter function after adding a "if" statement for identifying a tag or gameobject to detect.

How does one achieve this when using two separate OnTriggers like Enter and Exit.

Does that mean I have to use a function update if I don't create another separate script for the game object that has add force?

Thanks.

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 ComradeVanti · Apr 13, 2016 at 09:02 AM

So I dont really know Javascript.. so imma just answer in C#.. sorry. But it is pretty similar so.. you will be fine :)

If I understood you correctly you want to add force to an object when it enters a trigger. Here is what you wanna do:

 void OnTriggerEnter(Collider coll){
 if(coll.tag == "YourTag")
 coll.getComponent<Rigidbody>().addForce(yourVector);
 }

Sorry its not formatted... wrote it on the fly :)

So... does this help? :D

Comment
Add comment · Show 3 · 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 thePaintBrush · Apr 13, 2016 at 08:39 PM 0
Share

@Brullix3000 I looked at your example and thought maybe function would replace void, but then I have another issue.

 function OnTriggerEnter(other : Collider)
 {
     if (other.tag == "Player")
     {
             // value of 2 for Y axis to move up
         coll.getComponent<Rigidbody>().addForce(Vector3, 0, 2, 0);
         return;
       }
 }

This is a problem I have. It's one thing to have the trigger detect the player tag, it's another to have that trigger telling a cube to move up after the player comes into contact with the trigger. Don't understand how this could work because it seems to me there isn't any code that makes the trigger addForce to another object. And my goal with this is to make the trigger move a cube up and when the player exits the trigger, it moves down.

avatar image ComradeVanti thePaintBrush · Apr 14, 2016 at 04:56 PM 0
Share

In this case you would just replace

 coll.getComponent<Rigidbody>()

with

 theGameobjectYouWant.getComponent<Rigidbody>()

Then you do a similar thing with OnTriggerExit to move it down again.

If you need more detailed explanations of OnTriggerEnter etc. just search the documentation :)

avatar image thePaintBrush ComradeVanti · Apr 14, 2016 at 06:03 PM 0
Share

@Brullix3000

 function OnTriggerEnter(other : Collider)
 {
     if (other.tag == "Player")
     {
         moveCube.getComponent<Rigidbody>().addForce(Vector3, 0, 2, 0);
         return;
     }
 }

Unfortunately when I did this with positive and negative values with Enter and Exit functions I get errors.

Assets/trigger.js(5,50): BCE0043: Unexpected token: ). Assets/trigger.js(5,51): BCE0044: expecting ), found '.'. Assets/trigger.js(5,52): UCE0001: ';' expected. Insert a semicolon at the end.

Don't understand why it does this.

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

92 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 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 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 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

rigidbody.Addforce stacking? 2 Answers

Using OnTrigger to make two trigger objects open a door in unityscript. 0 Answers

Bullets falling through the terrain 2 Answers

How to set rigidbody velocity and angularVelocity to Vector3.zero over time? 1 Answer

Sphere with AddForce or Velocity not moving 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