r/UnityHelp

Rigidbody no collision despite colliders
▲ 3 r/UnityHelp+1 crossposts

Rigidbody no collision despite colliders

Player car

player car

cubes

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.

reddit.com
u/Bread_Jeremy — 8 days ago
▲ 2 r/UnityHelp+1 crossposts

Unity via Visual Scripting

Dear folks,
I'm an old time UE user yet nowadays I have to learn/relearn Unity but this time with Visual scripting. Any serious and deep visual scripting sources that you know and recommend? I don't know how long this project is actually but it has many branches and I may have to cover all of them by time. So I think my efforts in Unity will split my game engine life into 2 for a long time. So I am looking for a way to build a strong background. Any help? recommoendation? or Directions please?

reddit.com
u/DreamEonsVoyager — 8 days ago