Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 01, 2015 at 08:55 PM by Dave-Carlile for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by IceBeamGames · Dec 01, 2015 at 02:39 PM · rigidbody2dphysics2dboxcollider2d

Unity 2d Physics Collision Jumping / Glitching

I'm having trouble with a rotating canister object colliding with static box collider objects. Basically, I'm trying to replicate in 2D those explosive gas canisters from Half-Life 2 that push themselves around via escaping gas resulting in much hilarity and hi-jinx.

The canister is a 2d box collider with continuous collision detection enabled and a 2d rigid body and the background objects are static 2d box colliders. I'm using body.AddForceAtPosition(-gasEscapePosition * gasLeakForce, gasEscapePosition) to move the object around in FixedUpdate.

When the canister collides with any walls, its causing the object to jump for a frame and looks terrible. This has only been a problem since I started using AddForceAtPosition instead of AddForce.

Here is a gif of it jumping around: image link

Also, here is the movement code:

 using UnityEngine;
 
 public class CanisterHealth : Health
 {
     public override float maxHealth { get { return 30.0f; } set { } }
     public override float drain { get { return 0.0f; } }
 
     // Canister Variables
     public bool gasLeak = false;
     public float gasLeakForce;
     public float explosionDelay;
     public float timeToDie;
     public Vector2 gasEscapePosition;
     public float gasEscapeOffSet;
 
     // Vacuum variables
     public GameObject gasLeakParticles;
 
     /* Cached components */
     [HideInInspector] public Rigidbody2D body;
 
 
     public void Start()
     {
         // Cache component lookups
         body = GetComponent<Rigidbody2D>();
     }
 
     private void Update()
     {
         if (gasLeak && Time.time > timeToDie) Explode();
     }
 
     private void FixedUpdate()
     {
         if (gasLeak) GasLeak();
     }
 
     // Decrement the props's health by amount and set color
     public override void Damage(float amount, ContactPoint2D contactPoint)
     {
         // Flash body if damaged to give feedback
         if (health > 0)
         {
             if (!flashing) StartCoroutine(Flash());
             health -= amount;
         }
 
         // Flash body and reduce time to die if already leaking
         else if (gasLeak)
         {
             if (!flashing) StartCoroutine(Flash());
             timeToDie -= amount * 0.1f;
         }
 
         // Start gas leak
         else if (health <= 0 && !gasLeak && canDie)
         {
             Dying(contactPoint);
         }
     }
 
     public void Dying(ContactPoint2D contactPoint)
     {
         if (gasLeakParticles)
         {
             // Create gas escape position and direction
             gasEscapePosition = contactPoint.normal;
             float targetAngle = (Mathf.Atan2(gasEscapePosition.y, gasEscapePosition.x) * Mathf.Rad2Deg) + gasEscapeOffSet;
             Quaternion rotation = Quaternion.AngleAxis(targetAngle, Vector3.forward);
 
             // Create gas VFX and add as child of canister object so they remain attached
             GameObject vfx = Instantiate(gasLeakParticles, contactPoint.point, rotation) as GameObject;
             vfx.transform.parent = tr;
         }
 
         gasLeak = true;
         timeToDie = Time.time + explosionDelay;
     }
 
     private void Explode()
     {
         // Create delayed explosion and vacuum if an outside wall
         if (explosion != null) Instantiate(explosion, tr.position, Quaternion.identity);
 
         // Kill the poor object - :(
         Destroy(this.gameObject);
     }
 
     private void GasLeak()
     {
         // Add blow force from escape point
         body.AddForceAtPosition(-gasEscapePosition * gasLeakForce, gasEscapePosition);
     }
 }
 

Any ideas as to why?

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 MelvMay ♦♦ · Dec 01, 2015 at 03:04 PM 0
Share

Rigidbody2D.AddForceAtPosition uses world-space as the position. Your usage seems very odd in that you're using some world-space position * gasLeakForce as the force. Are you mixing-up world/local-spaces?

You can convert between global and local-spaces using Rigidbody2D.GetRelativePoint and Rigidbody2D.GetPoint.

avatar image IceBeamGames · Dec 01, 2015 at 08:54 PM 0
Share

Already solved this while I was waiting for it to be approved. It was a world and local space mix-up, yeah.

Thanks anyway $$anonymous$$elv.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What's a good way at keeping a ball bounce around inside the scene? 1 Answer

Why does poligon collider 2d restricts movement of box collider? 0 Answers

Platformer2D Player sticks to platform corners 0 Answers

Ball bounces at different heights when it shouldn't 0 Answers

BoxCollider2D + Edge radius + use auto mass 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