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 SavvyK · May 28, 2015 at 04:47 AM · c#physics2d

Bouncing square stops moving diagonally

In my current weekly project, I've got a square that bounces moves diagonally across the screen after a certain interval. During every game, at least one of these squares stops moving diagonally and instead only moves on the X or Y axis.

  • I have a physics component that has bounciness at 1 and friction at 0

  • The object has a rigidbody2d with gravity set to 0 and fixed angle turned on, no other changes.

  • I've attached the script I've been trying to use most recently to no avail

  • I've attempted to use Physics.Reflect but that doesn't seem to fix the issue as well.

Note, the issue normally happens when it is pinned between one object and a wall.

 public class BouncerObj : MonoBehaviour {
 
     Rigidbody2D rb2d;
 
     void Start()
     {
         rb2d = transform.GetComponent<Rigidbody2D>();
         rb2d.fixedAngle = true;
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if ((other.gameObject.tag == "Player" || other.gameObject.tag == "Enemy") && !gameObject.GetComponent<Collider2D>().isTrigger)
         {
             gameObject.GetComponent<Collider2D>().isTrigger = true;
         }
         else if ((other.gameObject.tag == "Friendly" || other.gameObject.tag == "Wall" || other.gameObject.tag == "Bouncer"))
         {
             Vector2 dir = new Vector2(-transform.position.x,-transform.position.y);
             GetComponent<Rigidbody2D>().velocity = dir;
         }
     }
 
     void TriggerChange()
     {
             gameObject.GetComponent<Collider2D>().isTrigger = true;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Player")
         {
             if (MovePlayer.invincible == true)
             { }
             else
             {
                 Scores.fShot += 5;
                 MovePlayer.invincible = true;
             }
         }
         else if ((other.gameObject.tag == "Friendly" || other.gameObject.tag == "Wall" || other.gameObject.tag == "Bouncer") && gameObject.GetComponent<Collider2D>().isTrigger)
         {
             gameObject.GetComponent<Collider2D>().isTrigger = false;
             Invoke("TriggerChange", 0.03f);
         }
     }
 }



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 SavvyK · Jun 01, 2015 at 06:13 AM 0
Share

Bumping. The week is over so I'm going to start something else, but I'm still curious as to what was causing the angles to change.

0 Replies

· Add your reply
  • Sort: 

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to disable script ,How to disable a script in multiple Gameobjects from another script? 1 Answer

Reduce Physics2D Lag 0 Answers

Making a bubble level (not a game but work tool) 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