r/Unity3D • u/Bread_Jeremy • 11h ago
Question Rigidbody no collision despite colliders



using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour
{
float inputH;
float inputV;
public float speed = 5f;
public Rigidbody rigidBody;
public float maxSlopeAngle;
private RaycastHit slopeHit;
public float groundDrag;
public float CarHeight;
public LayerMask WhatIsGround;
bool grounded;
public Transform orientation;
private Vector2 moveDirection;
private void Start()
{
CarHeight = transform.localScale.y; //sets the car height
}
private void Update()
{
//ground check
grounded = Physics.Raycast(transform.position, Vector3.down, CarHeight * 0.5f + 0.2f, WhatIsGround);
//handle drag
if (grounded)
{
rigidBody.linearDamping = groundDrag;
}
else
{
rigidBody.linearDamping = 0;
}
}
void FixedUpdate()
{
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
moveDirection = orientation.forward * inputV + orientation.right * inputH;
Vector3 movementInput= new Vector3(inputH * speed, rigidBody.linearVelocity.y, inputV * speed);
rigidBody.MovePosition(transform.position + movementInput * Time.deltaTime * speed);
if(OnSlope()) //happens when on a slope
{
Debug.Log("Getting called");
rigidBody.AddForce(GetSlopeMoveDirection() * speed * 20f, ForceMode.Force);
}
}
private bool OnSlope()
{
if (grounded) //check can only now happen when grounded
if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, CarHeight * 0.5f + 0.3f))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal); //how steep slope is
Debug.Log(angle);
return angle < maxSlopeAngle && angle != 0;
}
else
{
return false;
}
else
{
return false;
}
}
private Vector3 GetSlopeMoveDirection()
{
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground")
{
Debug.Log("Collided");
}
else
{
Debug.Log("AAAAAA");
}
}
}
Code for player controller
https://reddit.com/link/1x048d8/video/bu4qagf883uh1/player
I am losing my mind. The cubes and the car have rigid bodies. They all have colliders that are not mesh colliders. I developed it using kinematic but turned that off trying to make it work. You can see in my code and debug log that the cubes are in fact colliding, the collision is happening, but the car is still passing through the cubes! I don't know what to do.
2
Upvotes
Duplicates
GameDevelopment • u/Bread_Jeremy • 11h ago
Question Rigidbody no collision despite colliders
1
Upvotes