Is there a way to make my Player can’t passthrough a wall?
For now, it’s working, but players can still break through walls
I know I can change the rigidbody into Dynamic. it’s just I really curious is there a way to make this happen?
Here’s my code for player collision, can I do better with this code?
// Update is called once per frame
void FixedUpdate()
{
Movement();
temPos = direction * Time.deltaTime * speed;
}
void Movement()
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
direction = Vector2.zero;
direction.x = x;
direction.y = y;
transform.Translate(temPos);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Walls"))
{
temPos = -temPos;
}
}
Of course. This is how Kinematic controllers work. Dynamic gives you collision response, the whole point of Kinematic is to turn that off and give you explicit control.
You use physics queries to detect your environment and make decisions on how to move.
Triggers only tell you that you’re overlapped, they don’t provide any more detail such as contact points or normals which are need to make decisions on how to move.
That cannot be described fully here, you need to create or reuse an existing kinematic controller or learn how to use queries to detect your environement. Normally you perform a query along the direction you want to move before you move and then figure out how far you can move in that direction.
NOTE: It looks like you’re using 2D physics so know that “IsKinematic” is an option that is deprecated there. You can explicit select the body-type on the Rigidbody2D.
What part didn’t you understand? Physics queries? The environment around your player?
I don’t know how else to state it. You’re using a Kinematic body. You need to figure out where it can move before you move. You use physics queries to do that.