Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 egonspengler_84 · May 10, 2021 at 03:21 AM · rigidbody2dcollision detectionraycasthit2dkinematic

Question about raycast collision detection involving Kinematic Rigidbody2D

Hi there,

So in my scene I have two game objects - one is a square and the other is a wall that is located to the right of it.

Upon running the project the square travels to the right along the X-axis using the transform.Translate function. When a raycast that shoots from the side of the square hits the wall the square is meant to travel the value that is stored in "hit.distance". Once this happens the square will stop and rest flush against the wall.

However when I run the project the code only works when there is a Kinematic Rigidbody2D attached to the square. Whereas if I removed the Rigidbody I get very unpredictable behaviour when the raycast hits the wall. I don't understand why this is? Why is a Kinematic Rigidbody2D needed for the code to work in a predictable manner?

Here is a video demonstrating how running the code with and without the Rigidbody2D affects the wall: https://www.youtube.com/watch?v=8FpQH4qx-6k&ab_channel=segoviate

And here is the code that is attached to the square:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player2 : MonoBehaviour
 {
     public Vector2 botRight;
     private BoxCollider2D _col2D;
     public LayerMask _whatIsWall;
     public float force = 0.015f;
 
     // Start is called before the first frame update
     void Start()
     {
         _col2D = GetComponent<BoxCollider2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         botRight = new Vector2(_col2D.bounds.max.x,_col2D.bounds.min.y);
         RaycastHit2D hit = Physics2D.Raycast(botRight,Vector2.right,1f,_whatIsWall);
         Debug.DrawRay(botRight,Vector2.right * 1f,Color.red);
 
         if(hit){
 
             transform.Translate(hit.distance,0,0);
 
         }else{
 
             transform.Translate(force,0,0);
             
         }
     }
 }
 

Any helpful information would be appreciated.

Kind regards

Comment
Add comment · Show 5
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 GSGregory · May 10, 2021 at 04:37 AM 0
Share

My guess is that the Rigidbody is keeping the box from traveling through the collider on the wall.

The weird issue with moving though is likely caused by transform.Translate. It uses Space.Self by default aka local space vs world space. So add in transform.Translate(hit.distance, Space.World)

avatar image egonspengler_84 GSGregory · May 10, 2021 at 04:42 AM 0
Share

I've added the code that you've suggested but it gives me a red squiggly line in my IDE as you can see here: https://ibb.co/P4XfC5D

avatar image GSGregory egonspengler_84 · May 10, 2021 at 04:49 AM 0
Share

Ah whoops.

So it should be transform.Translate(hit.distance,0,0,Space.World)

Where its (x,y,z,Space.World or Space.Self)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vargata · May 10, 2021 at 05:19 AM

huhhhh? I see you are trying to solve this for a while and keep coming up with stranger and stranger ideas....

in general a collider with a rigidbody is more dynamic and is checking hits more precisely. that's why it helps. you don't need it though, just change Update() to FixedUpdate(); and it will yield a near perfect raycastcheck every time.

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

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

125 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

Related Questions

Issue with checking to see whether the Bounds value's of two game objects equal each other 1 Answer

Player not taking damage on collision with enemy 1 Answer

Interpolation issue, some items not being interpolated correctly. 0 Answers

The character velocity changes while the rigidbody is kinematic 1 Answer

Kinematic rigidbody movement. 2 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