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 Peange · Jul 13, 2010 at 11:10 PM · collisionphysicscharactercontroller

Double collision with Character Controller?

Hi folks, i'm relatively new to programming and even more so with Unity.

What i'm trying to do is create Pac-man for a University Coursework. I have quite a lot of it working, however a new problem has arisen and I don't know why.

Pac-man used to have a rigidbody attached to handle the collisions, however due to the forces applied (I think?) he kept bouncing off the maze walls. On further research I found the Character Controllers do not take forces into account. All was working well until I realised that more often than not double the score was given for each pellet consumed (sometimes the correct score was given). This only happened after I changed to Character Controller.

I've checked the log and it is incrementing by 100 but incrementing twice per collision. Any help is greatly appreciated.

Here's my Consume code:

private var score : int = 0; private var stringScore : String; private var scoreObject : GameObject;

scoreObject = gameObject.Find("Score");

function OnControllerColliderHit(hit : ControllerColliderHit) { var collide = hit.gameObject; if(collide.tag == "Pellet") { Destroy(collide.gameObject); score += 100;

     Debug.Log(score);
 }
 stringScore = score.ToString();
 scoreObject.GetComponent(TextMesh).text = stringScore;

}

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
1

Answer by jc_lvngstn · Jul 29, 2010 at 02:23 PM

Just disable the pellet collider when you eat it, boom no more collisions and it will eventually get destroyed.

I don't have Unity if front of me, but it should be something along the lines of gameObject.Collider.Active = false

Or...just disabling the whole pellet might do the trick also.

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 Mike 3 · Jul 13, 2010 at 11:56 PM

Try DestroyImmediate instead of Destroy - it could be that your other object is hanging around for two fixedupdates before being deleted (which is feasible - you get 0 to lots of fixed updates every frame, depending on the timing)

Edit, since that failed spectacularly:

collide.transform.position.y -= 10000;
Destroy(collide.gameObject);

that should just move the other object far away from the play zone so it can't be collided with twice

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 Peange · Jul 14, 2010 at 12:02 AM 0
Share

Thanks but i've tried DestroyImmediate and Unity crashes =/ Is there any other way to do this? I also tried "yield" before the Destroy and no luck

avatar image
0

Answer by karl_ · Oct 08, 2010 at 11:58 PM

Instead of using OnControllerColliderHit try an OnTriggerEnter or OnCollisionEnter. This will ensure that your if statement is only called once.

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 javanoob · Mar 03, 2013 at 12:26 PM 0
Share

I have the same issue but unfortnately what you said $$anonymous$$arl still has the double hit problem ?

here is my code snippet ,any ideas ?

 function OnTriggerEnter(hit : Collider){
     
 if (hit.gameObject.name == "Cube1"){
               
            ckp=ckp+1;
         GameObject.Find("Cube2").transform.Translate(0, -1000, 0);
         GameObject.Find("Cube1").transform.Translate(0, 1000, 0);
         
         }
 
  else if (hit.gameObject.name == "Cube2"){
              
            ckp=ckp+1;
         GameObject.Find("Cube3").transform.Translate(0, -1000, 0);
         GameObject.Find("Cube2").transform.Translate(0, 1000, 0);
 }

etc

sometimes ,randomly it moves the cube objects down twice 2000 ins$$anonymous$$d of 1000 ?

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

1 Person is following this question.

avatar image

Related Questions

Keeping object with character controller from staying on top of another character controller object 4 Answers

OnCollisionEnter not working with CharacterController 1 Answer

Is there a rigidbody character controller 3rd person that can handel stairs (steps)? 1 Answer

Can't use Rigidbody or Character Controller 0 Answers

How can I make two Character Controllers Collide 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