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 Raceandemergency · Feb 15, 2014 at 07:23 PM · movementcollider

Unity 2D stop player when hit collider

Hy there. First, sorry for my bad english, im not native. Second: I few days ago i start playing around with Unity Third: I want to stop my player when he hits a collider. I gave him the static variable "Player" in a other code. But in this code i can't stop his movement. The code from " function OnTriggerEnter2D (hitInfo : Collider2D) { if (hitInfo.name == "Player") { Debug.Log ("Game Over");" works fine. Here is my code:

 #pragma strict
 
 var "Player" : Rigidbody2D;
 
 function OnTriggerEnter2D (hitInfo : Collider2D) {
     if (hitInfo.name == "Player") {
         Debug.Log ("Game Over");
         rigidbody2D.velocity.x = 0;
         rigidbody2D.velocity.y = 0;
     }
 }

I hope you can help me.

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 robertbu · Feb 15, 2014 at 07:29 PM

Untested, but I believe this is what you want:

 #pragma strict
  
 var "Player" : Rigidbody2D;
  
 function OnTriggerEnter2D (hitInfo : Collider2D) {
     if (hitInfo.name == "Player") {
        Debug.Log ("Game Over");
        hitInfo.rigidbody2D.velocity = Vector2.zero;
     }
 }

That is, you want to use the hitInfo to address the player's rigidbody2d, not set the rigidbody2d on the object this script is attached to.

Comment
Add comment · Show 2 · 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 Raceandemergency · Feb 15, 2014 at 09:23 PM 0
Share

It seems logical to me, but it does not work. $$anonymous$$aybe it helps to know the other code:

 #pragma strict
 var moveUp : $$anonymous$$eyCode;
 var moveRight : $$anonymous$$eyCode;
 var moveLeft : $$anonymous$$eyCode;
 var moveDown : $$anonymous$$eyCode;
  
 
 static var String = "Player";
 
 
 
 function Update () 
 {
     if (Input.Get$$anonymous$$ey (moveUp) )
     {
         rigidbody2D.velocity.y = 1 * 2;
     }
     else if (Input.Get$$anonymous$$ey (moveRight) ) 
     {
         rigidbody2D.velocity.x = 1;
     }
     else if (Input.Get$$anonymous$$ey (moveLeft) )
     {
         rigidbody2D.velocity.x = 1 -2.5; 
     }
     else if (Input.Get$$anonymous$$ey (moveDown) )
     {
         rigidbody2D.velocity.y = 1 * -2;
     }
     else
     {
         rigidbody2D.velocity.y = 0;
         rigidbody2D.velocity.x = 1;
     }
 }
avatar image robertbu · Feb 15, 2014 at 09:30 PM 0
Share

The problem is that your code on your player will move s$$anonymous$$dily to the right when no key is pressed. The code you first posted will stop the object, but your code on the actual player resets that movement to (1,0) on the next update call, so it appears as if the first code does nothing. I'm not sure how to suggest you solve this problem since I don't know the mechanics of your game. You may want to add an OnTriggerEnter() to the player script that sets the idle movement to (0,0), and an OnTriggerExit() to set the idle movement back to (1,0). Or you may want to add a Raycast() to the second script that stops moving if an object is too close.

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

18 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

Related Questions

Detecting what surface character is moving on and restrict movement outside 1 Answer

How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers

Wacky helicopter Please Help! 0 Answers

Cannot move FPSController after respawning at a spawn point. 1 Answer

Moving up and down (in a slope) without change to velocity or "bumping" 0 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