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 /
avatar image
1
Question by Jamiemon · Mar 17, 2017 at 02:41 AM · collidersjavaoncollisionenterdetection

How to detect if a collider is touching a gameobject (java)

I've looked up multiple things, but none of them seem to work. I keep getting errors saying things like it doesn't recognize "col" as well as there being an unexpected token >.> All I want this snipit of script I have pasted to do is make the object go up when touching the ground

function Update () { void OnCollisionEnter(Collision col){ if(col.gameObject.name == "grassTerrain"){ transform.position.y = transform.position.y + 0.02; } } }

Any help is much appreciated ^-^

Comment
Add comment · Show 1
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 toddisarockstar · Mar 17, 2017 at 03:24 AM 0
Share

in unitys javascript/java there is no "void". you would replace "void" with "function"

also the OnCollisionEnter would not go inside the update funtion brackets. its its own thing.

I highly recommend learning C# and writing with that because its an industry standard language that is good to know and more people will offer help here.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Commoble · Mar 17, 2017 at 03:21 AM

OnCollisionEnter doesn't go inside Update, it's its own function.

You have:

 void Update ()
 {
     void OnCollisionEnter(Collision col)
     {
         if(col.gameObject.name == "grassTerrain")
         {
             transform.position.y = transform.position.y + 0.02;
         }
     }
 }

You should have:

 void Update ()
 {
 // nothing in here yet
 }
 
 
 void OnCollisionEnter(Collision col)
 {
     if(col.gameObject.name == "grassTerrain")
     {
         transform.position.y = transform.position.y + 0.02;
     }
 }

This is one problem fixed, but your script still isn't valid, because you can't alter the x/y/z values of vectors in Transforms directly, you have to alter the whole vector, so you do this:

 void Update ()
 {
 // nothing in here yet
 }
 
 
 void OnCollisionEnter(Collision col)
 {
     if(col.gameObject.name == "grassTerrain")
     {
         Vector3 oldPos = this.transform.position;
         this.transform.position = new Vector3(oldPos.x, oldPos.y + 0.02F, oldPos.z);
     }
 }

The code in this script is now valid. I recommend against comparing strings in a collision or update event, and you probably won't notice the object going up by just 0.02 units, but it is valid code.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider waiting for two differently tagged game objects to enter before doing something 1 Answer

How to make objects collide once 1 Answer

Precise Collision Detection 0 Answers

collision detection 4 Answers

Collision detection when no collision happens,OnCollision is called without a collision 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