Box collider funktioniert nicht mit rb.MovePosition?
Ich habe mit Unity angefangen und habe ein kleines problem:
Und zwar habe ich eine kamera mit boxcollider und einen terrain mit terraincollider. Nur leider kann ich die kamera durch das terrain durchschieben! Ich habe schon den verdacht dass es an rb.MovePosition liegt aber weiß nicht wie ich das reparieren kann! Alles wie zumbeispiel rigidbody sind auf der kamera da bin ich mir sicher! Es kommt auch kein Fehler in der Konsole!
Mein Kompletter code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothMove : MonoBehaviour
{
public float rotationSpeed = 2.0f;
public float smoothRotationFactor = 5.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public float jumpForce = 10.0f;
public float moveSpeed = 20.0f;
private Rigidbody rb;
private bool isMovingForward = false;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
HandleCameraRotation();
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
StartMovingForward();
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
StopMovingForward();
}
if (isMovingForward)
{
MoveForward();
}
}
private void HandleCameraRotation()
{
if (Input.GetMouseButton(0))
{
yaw += rotationSpeed * Input.GetAxis("Mouse X");
pitch -= rotationSpeed * Input.GetAxis("Mouse Y");
Quaternion targetRotation = Quaternion.Euler(pitch, yaw, 0.0f);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * smoothRotationFactor);
}
}
private void Jump()
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y + 5, rb.velocity.z);
}
private void StartMovingForward()
{
isMovingForward = true;
}
private void StopMovingForward()
{
moveSpeed = 20f;
isMovingForward = false;
}
private void MoveForward()
{
moveSpeed += 0.1f;
Vector3 forwardDirection = transform.forward;
forwardDirection.y = 0.0f; // Keep movement on the horizontal plane
rb.MovePosition(rb.position + forwardDirection * moveSpeed * Time.deltaTime);
}
}
Ich würde mich sehr freuen wenn ihr mir helft. Danke :)