#I have a 3rd person camera and I would like to block the camera in the room so that it cannot
1 messages · Page 1 of 1 (latest)
The title got cut off, what was the full question?
Sorry! I would like to block the camera in my dungeon but I can't do it, here's the problem, it's when my player arrives at a wall the camera goes out and sees the outside and I don't want
Im not sure if this solution is compatible with cinemachine, ive never used it before, but here is what i did to solve that issue:
From the point the camera orbits, do a spherecast towards the target camera position. If the ray hits a position either place the camera in that spot instead or move it slightly closer from there to make extra sure it doesnt clip
I didn't understand well
Oh you arent using any scripts for the camera, Like i said i dont know much about cinemachine. It might have a build-in method for doing something like this
Oh sorry I didn't understand, did you code it yourself?
Did you put a script on camera directly?
Yeah I made my own solution. But good new! it seems cinemachine has a way to have this done automatically
https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/CinemachineCollider.html
This is probably what you are looking for
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))]
public class PlayerBehavior : MonoBehaviour
{
[Header("Movement Settings")]
[SerializeField] private float moveSpeed = 3f;
[SerializeField] private float rotationSpeed = 6f;
[Header("Optional Particles")]
[SerializeField] private ParticleSystem walkingParticles;
private Rigidbody rb;
private Animator animator;
private Vector3 moveInput;
private void Awake()
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
animator = GetComponent<Animator>();
}
private void FixedUpdate()
{
Vector3 direction = new Vector3(moveInput.x, 0f, moveInput.z);
if (direction.magnitude > 1f)
direction.Normalize();
Vector3 targetPosition = rb.position + direction * moveSpeed * Time.fixedDeltaTime;
targetPosition.y = rb.position.y;
rb.MovePosition(targetPosition);
if (direction.sqrMagnitude > 0.01f)
{
Quaternion targetRotation = Quaternion.LookRotation(direction);
rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime));
}
float speed = new Vector2(direction.x, direction.z).magnitude;
animator.SetFloat("Speed", speed);
// Particules de marche
if (walkingParticles != null)
{
if (speed > 0.1f && !walkingParticles.isPlaying)
walkingParticles.Play();
else if (speed <= 0.1f && walkingParticles.isPlaying)
walkingParticles.Stop();
}
}
public void Move(InputAction.CallbackContext context)
{
Vector3 v = context.ReadValue<Vector3>();
// Inversion de l’axe Z pour Z = haut / S = bas
moveInput = new Vector3(v.x, 0f, -v.y);
}
}