- Home /
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
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)
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
Ah whoops.
So it should be transform.Translate(hit.distance,0,0,Space.World)
Where its (x,y,z,Space.World or Space.Self)
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.
Your answer
Follow this Question
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