Home > OS >  collision not working properly in unity when moving
collision not working properly in unity when moving

Time:09-09

so I have rigid body and when it collides with another body with low speeds it is working just fine but when it collides with hight speed it goes through the object I've Been have this problem for day and I can't fix it

here's my code

this is my player movement file

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharcterController : MonoBehaviour
{
    // Start is called before the first frame update
    public Vector3 PlayerMovementVar;

    public Rigidbody Rigidbody_comp;
    // Start is called before the first frame update
    void Start()
    {
        Rigidbody_comp = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMovement();
        
    }

    void PlayerMovement()
    {
        float horizontalAxis = Input.GetAxis("Horizontal")/30;
        float verticalAxis = Input.GetAxis("Vertical")/30;
        PlayerMovementVar = new Vector3(horizontalAxis,0f,verticalAxis);
        transform.Translate(PlayerMovementVar,Space.Self);
    }
}

CodePudding user response:

I shouldn't be the one answering this since I have very little knowledge of unity but I'm pretty sure it might be because you are using transform.Translate which I think it avoids collisions try using a character controller instead :)

CodePudding user response:

If you want to use properly Unity physics system you must use Forces instead of Translate direcly your gameobject. Try this:

Rigidbody_comp.AddForce(PlayerMovementVar);

instead of

transform.Translate(PlayerMovementVar,Space.Self);

CodePudding user response:

RigidBody movement and Transform movement are different from each other, each causes a different type of movement. when moving with a rigidbody, in order to use its physics ability, you should move it instead of the usual transform.Translate.

Rigidbody_comp.AddForce(PlayerMovementVar);
  • Related