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 Kalvin_One · Feb 20, 2014 at 02:48 PM · 2drigidbody2daddforceboxcolliderinput.getkey

Movement stops working randomly

Hi, I'm developing a maze game and currently I'm working on coding the player. So far i've created a simple maze in which the player moves around. I've taken a few snippets from here and there.

The player moves forward until it reaches a collider, when it stops. I've used AddForce since the camera moves and rotates with the player so the world axis won't help.

 #pragma strict
 
 var rotateCounter : KeyCode;
 var rotateClock : KeyCode;
 var forwardMove : KeyCode;
 
 function Update () {
     if (Input.GetKeyDown(forwardMove)){
         rigidbody2D.AddForce(new Vector2(transform.up.x*700, transform.up.y*700));                                  
    } 
 
 if (Input.GetKeyDown(rotateCounter)) {
 rotateLeft();
 }
 if (Input.GetKeyDown(rotateClock)) {
 rotateRight();
 }
 }
 
 function rotateLeft() {
 for(var i = 0; i<90; i+=2) {
 yield;
 transform.Rotate(0,0,2);
 }
 }
 
 function rotateRight() {
 for(var i = 0; i>-90; i-=2) {
 yield;
 transform.Rotate(0, 0, -2);
 }
 
 }

In a separate file, i've added the collision controls

 function Update () {
     if ( rigidbody2D.velocity.magnitude > 1 ){
         Debug.Log("Moving");
         var MoveScript = GetComponent.<MovementScript>();
         MoveScript.enabled = !MoveScript.enabled;
     } 
 }
 
 function OnTriggerEnter2D(other: Collider2D) {
     Debug.Log("Collision");
     //The commented solution works fine too to stop the player
     //rigidbody2D.AddForce(new Vector2(transform.up.x*700*-1, transform.up.y*700*-1));
     rigidbody2D.velocity = Vector3.zero;
 }

The second file disables any input until the player reachs a wall in order to prevent rotations in the middle of a run and here comes my problem. I can wander arond a few times and then suddently stops working. My input doesn't come back so the game "freezes"

I don't want to add rigidbodys to the walls since the colliders are a few pixels separated from the "physical" walls.

What can make my code stop working? Thank you in advance for your help :)

Comment
Add comment · Show 2
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 robertbu · Feb 20, 2014 at 04:16 PM 1
Share

Is the 'Debug.Log("Collision")' happening? if so, why not enable the script there:

  rigidbody2D.velocity = Vector3.zero;
  GetComponent($$anonymous$$ovementScript).enabled = true;
avatar image Kalvin_One · Feb 21, 2014 at 02:54 PM 0
Share

THAT just worked! thank you very much :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aridez · Feb 20, 2014 at 03:55 PM

You could try using velocity instead of addforce:

   if (Input.GetKeyDown(forwardMove)){
        rigidbody2D.velocity = new vector2(5, 5); //5 is just an arbitrary value change it so it suits you
 }

I would also move this code to fixedupdate() instead of update(): Look here And hope this helps!

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 Kalvin_One · Feb 21, 2014 at 02:53 PM 0
Share

Velocity doesn't works for me since I need a relative value of X and Y. The camera rotates with my character so the world's axis may differ from the player's axis.

Still, I changed from Update() to FixedUpdate(), thank you very much!

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

19 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

Related Questions

Rigidbody2d.AddForce behaving odd 1 Answer

Rigidbody2d addforce isn't moving character 1 Answer

Shoot logic, bullet instantiation 1 Answer

Need help on my script to calculate gravity for a mass 1 Answer

Rigidbody2D.velocity.x not working but Y does 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