M
M
mehonoshin212022-02-04 13:33:48
C++ / C#
mehonoshin21, 2022-02-04 13:33:48

I have a 3D player in Unity3d that passes through objects, what should I do?

They have BoxCollider and Rigidbody and the player has the same. Here is the script. Help me please!

spoiler

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Controller : MonoBehaviour {
private Rigidbody rb;

[SerializeField]
private float speed = 5.0f;

[SerializeField]
private float lookSpeed = 3.0f;

private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;


void Start() {
rb = this.GetComponent();
}

public void Move (Vector3 _velocity){
velocity = _velocity;
}

public void Rotate (Vector3 _rotation){
rotation = _rotation;
}

void FixedUpdate () {
PerformMove ();
PerformRotation ();
}

void PerformMove () {
if (velocity != Vector3.zero)
rb.MovePosition (rb.position + velocity * Time.fixedDeltaTime);
}

void PerformRotation () {
rb.MoveRotation (rb.rotation * Quaternion.Euler (rotation));
}

void Update() {
float xMov = Input.GetAxis ("Horizontal");
float zMov = Input.GetAxis ("Vertical");

Vector3 movHor = Vector3.right * xMov;
Vector3 movVer = Vector3.forward * zMov;

Vector3 velocity = (movHor + movVer).normalized * speed;

Move (velocity);

float yRot = Input.GetAxis ("Mouse X");
Vector3 rotation = new Vector3 (0f, yRot, 0f) * lookSpeed;

Rotate (rotation);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-02-04
@freeExec

And why would he stumble on the walls if you turned on the kinematics and directly control the object, and does not use physics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question