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 AceDawg45 · Jun 05, 2014 at 09:40 PM · javascriptcollisionbeginnersimple

Please help with a simple collision.

I have tried every script out there on the internet. None of them work. All I am trying to do is have a collision between a cylinder and first person controller. Pretty much, when the Player hits the cylinder, the Log says something. That isn't what it is supposed to do, but all I want to do is get this to work. Please help.

My code:

 function OnCollisionEnter(theCollision : Collision){
     if(theCollision.gameObject.tag == "Enemy"){
         Debug.Log("fjeoijfoiejfoiejf");
     }
 }

This is any and all code I have for the entire thing.

Screenshot of unity (red text was edited in) ![alt text][1] [1]: /storage/temp/27333-src.jpg

src.jpg (300.4 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 05, 2014 at 09:42 PM

Character Controllers are weird. They don't use the standard callbacks.

If you want to know if the charter controller has collided with an object you need to use an OnControllerColliderHit callback http://docs.unity3d.com/ScriptReference/CharacterController.OnControllerColliderHit.html

I just don't use them, I build my own controllers using real Unity physics.

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

Answer by AceDawg45 · Jun 05, 2014 at 09:59 PM

Hmmm.... Didn't work for me. My code was:

 function OnControllerColliderHit(hit : ControllerColliderHit){
     if(hit.gameObject.tag == "Enemy"){
         Debug.Log("feee");
     }
 }

Also, I have the script attached to my character. Is this right?

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 Jeff-Kesselman · Jun 05, 2014 at 10:01 PM 0
Share

Yes it should be fine on your character as long as it is on the same gameObject as the character controller

Try this to see if you are getting the collision:

 function OnControllerColliderHit(hit : ControllerColliderHit){
     Debug.Log("In controller collider");
     if(hit.gameObject.tag == "Enemy"){
        Debug.Log("feee");
     }
 }
avatar image AceDawg45 · Jun 05, 2014 at 10:05 PM 0
Share

No, won't even get the collision

avatar image AceDawg45 · Jun 05, 2014 at 10:09 PM 0
Share

Ugh. FINALLY. I got it. For now. It took me 3 HOURS for that. Unity is gonna be a lot more tricky than I thought.... I am hardcore C style programmer (C++ C# C) and this is a lot different.

avatar image AceDawg45 · Jun 05, 2014 at 10:33 PM 0
Share

Okay, new question, same idea, problem with collisions. No need to post new thread about it. But, I have a var named health (it is an integer set to 100). It is global, and inside the function (you wrote it in the comment) if statement, I have health = health - 10; That way, when you touch the enemy, you lose 10 health. Also, inside the Debug.Log I have your health displayed ins$$anonymous$$d of "feee". Well, I lose like 50 health every time I touch it. Do you know how this happens? Full code (now): var health = 100; //global variable

 function OnControllerColliderHit(hit : ControllerColliderHit){
     
     if(hit.gameObject.tag == "Enemy"){
         health = health - 10;
        Debug.Log(health);
     }
     if(health < 0){
         Destroy(gameObject);
     }   
 }
 
avatar image Jeff-Kesselman · Jun 05, 2014 at 10:51 PM 0
Share

If you already knwo C/C#, DONT use UnityScript.

It will give you more pain with less erro info because it is so loosely typed and it lacks the structural support of a real language.

use C# you will be much happier.

I encourage people who are learning Unity to skip UnityScript and just go to C# for the same reasons.

Show more comments
avatar image
0

Answer by T1Green · Jun 05, 2014 at 11:02 PM

Your script seems fine. What you are missing is the "Enemy"-tag on the cylinder.

At the screenshot your "Enemy1" is labelled as "Undefined" under tag.

Make a new one called "Enemy" if it doesn't exist already and then remember to add the tag to the cylinder.

That should make it work.^^

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 AceDawg45 · Jun 06, 2014 at 07:10 PM 0
Share

Yeah I didn't notice that until after Jeff posted his code. Thanks anyway!

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

Overlap Detection HELP!! 0 Answers

How to switch off two lights? 2 Answers

function OnCollosionEnter problems 1 Answer

Collide detection with tag [JS] 0 Answers

OnTriggerEnter Or OnCollisionEnter? 3 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