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 /
avatar image
0
Question by Christiansquare · Jan 17, 2020 at 11:03 PM · rigidbody2daddforceboxcollider2d

Rigidbody2D stuck on Box Colliders

I have a camera with a box collider and Rigidbody2D (The character). This camera contains the following script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraMover : MonoBehaviour
 {
     public float speed; // set to 5 in editor
     public float accelleration; // set to 3 in editor
     public float jumpSpeed; // set to 10 in editor
     public float sprintMultiplier; // set to 2 in editor
     Vector3 tempVelocity;
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             tempVelocity.Set(0, jumpSpeed, 0);
             this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
         }
         else
         {
             if(Input.GetKeyDown("a") && !Input.GetKey("d"))
             {
                 tempVelocity.Set(-speed, 0, 0);
                 this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
             }
             if(Input.GetKeyDown("d") && !Input.GetKey("a"))
             {
                 tempVelocity.Set(speed, 0, 0);
                 this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
             }
             if(Input.GetKey("a") && !Input.GetKey("d"))
             {
                 tempVelocity.Set(-accelleration * Time.deltaTime, 0, 0);
                 this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
             }
             if(Input.GetKey("d") && !Input.GetKey("a"))
             {
                 tempVelocity.Set(accelleration * Time.deltaTime, 0, 0);
                 this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
             }
             if(Input.GetKeyUp("a") || Input.GetKeyUp("d"))
             {
                 tempVelocity.Set(-this.GetComponent<Rigidbody2D>().velocity.x, 0, 0);
                 this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
             }
         }
         if(Input.GetKeyDown(KeyCode.LeftShift))
         {
             speed *= sprintMultiplier;
             accelleration *= sprintMultiplier;
         }
         if(Input.GetKeyUp(KeyCode.LeftShift))
         {
             speed /= sprintMultiplier;
             accelleration /= sprintMultiplier;
         }
     }
 }
 

After falling a short distance, the character collides with one or more sprites that I have created which also contain box colliders in the 2D variant. When moving left or right using the "a" and "d" keys, the character is occasionally stopped, but only while colliding with the sprites. What could be changed in order for the character to keep moving while colliding with these other sprites?

Apologies if the content of this question is difficult to read, this is my first question on this forum.

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 JasonBennett · Jan 23, 2020 at 05:15 PM 0
Share

Hi @Christiansquare,

I think I understand, but need a bit of clarification. You'd like the player to collide with the sprites, but push them aside? Or would you prefer to have the player pass through the sprites?

-Jason

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Christiansquare · Jan 23, 2020 at 05:16 PM

With a constant velocity (without using acceleration as in the script above), the character no longer gets stuck.

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 mscardinal · Jan 18, 2020 at 06:34 PM

@Christiansquare Looks like an easy to fix problem. The collider of the object that stops the caracter from moving has to be set to trigger.

Comment
Add comment · Show 18 · 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 Christiansquare · Jan 18, 2020 at 10:50 PM 0
Share

Doing that causes the character to not physically collide with the objects. I would like for the character to collide with the objects. However, the character gets stuck as if it is against a wall when moving on top of the objects and there is no wall. How may I post a video to this forum?

avatar image mscardinal Christiansquare · Jan 19, 2020 at 10:46 AM 0
Share

i dont know how to post a video. But what are you actually trying to do ? I think some pictures could help.

avatar image Christiansquare mscardinal · Jan 19, 2020 at 10:02 PM 0
Share

Here are some pictures: https://imgur.com/a/eTf0X$$anonymous$$0 Note that while the "stopped" velocity is not 0, the character does not move at all.

Show more comments

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

122 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with 2 Rigidbodies having respective colliders (2d) 1 Answer

Rigidbody2D linearDrag - Suddenly Change 0 Answers

Enemy AI 2D not working? ( Rigidbody.Addforce() ) 2 Answers

How do i make my grounded variable change as I hit the platform and as I jump? 2 Answers

Rigidbody2d.AddForce behaving odd 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