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 Revearn · Jan 28, 2021 at 02:12 AM · collision2d-platformercollider2d2d-physics

RigidBody2D mouse going through Collider ,Rigidbody2D move with mouse

I am in the progress of making a 2D game where the player can shoot out an orb that can be controlled via the mouse. Currently, the prefab of the orb has a rigidbody2D and a circle collider 2D. The tile collider on the level seems to be able to stop the prefab at first, however, when I drag my mouse at a faster speed it punches straight through. I have been told that I need to not use transform and I edited my code but to no use. Any suggestions?

 public class Memory : MonoBehaviour
 {
     private Vector3 mousePosition;
     public float moveSpeed = 0.1f;
     private Rigidbody2D rb2D;
     
     // Use this for initialization
     void Start () {
         rb2D = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate () {
         
             mousePosition = Input.mousePosition;
             mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
             rb2D.position = Vector2.Lerp(rb2D.position, mousePosition, moveSpeed);
         
     }
 }
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
1
Best Answer

Answer by VeryAnnoyingCat · Jan 28, 2021 at 05:58 PM

First of all set the rigidbody collision detection of the orb to continuous. Then use this code: Hopefully you know how to use layer masks.

 private Vector3 mousePosition;
 public float moveSpeed = 0.1f;
 public float maxSpeed;
 private Rigidbody2D rb2D;
 private Collider2D c2D;
 public LayerMask collidable; // list of things that are collidable with. !!!Set the orb on its separate layer and unckeck it it the layermask!!!
 private RaycastHit2D collidingCheck;
 
 // Use this for initialization
 void Start()
 {
     rb2D = GetComponent<Rigidbody2D>();
     c2D = GetComponent<Collider2D>();
 }
 
 void Update()
 {
 
     mousePosition = Input.mousePosition;
     mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
     collidingCheck = Physics2D.Raycast(transform.position, ((Vector2)(mousePosition - transform.position)).normalized, ((Vector2)(mousePosition - transform.position)).magnitude, collidable);
     //checks if anything is between  orb and cursor
 
     if (c2D.IsTouchingLayers(collidable) == false || collidingCheck.collider == null) //checks if orb is touching something or if something is between it. Does not allow movement if both.
     {
         rb2D.position = Vector2.Lerp(rb2D.position, mousePosition, moveSpeed);
     }
 }

By the way, this will still not work at high speeds so set move speed to a low value.

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 Revearn · Jan 30, 2021 at 12:03 AM 0
Share

Thanks for the advice! I'll try it out soon!

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

189 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 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

CircleCollider2D vs PolygonCollider2D: which collision solving is less expensive? 1 Answer

Per-Sorting Layer Collision 1 Answer

[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers

Side Collision on "One Way" platform? 2 Answers

Player Gets Stuck On TilemapCollider2D 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