Hey guys, my player at the moment rotates to the position of the mouse, but when the mouse gets behind a mountain, the player stops rotating to the mouse and when I get out of the mountain it again starts following. Does someone know how the player can also rotate to the mouse position when the mouse is behind the mountain? (It’s a 3D game)
Take a look at LayerMask; it allows you to set objects in a layer that the raycast would then ignore, and effectively “go through” them.
I’d suggest setting the mask as a property or field that’s exposed to the Inspector for easier editing. Then your code would look something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayCamera : MonoBehaviour
{
public Camera aremac;
public LayerMask hitMask; // define this in the inspector! remember that checkmarks mean layers to hit
public float rayRange = float.MaxValue; // making this a public field is optional, but the value is needed for the Raycast call
void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
}
void FixedUpdate()
{
RaycastHit hit;
Ray ray = aremac.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, rayRange, hitMask))
{
transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z)); // Do something with the object that was hit by the raycast.
}
}
}
Thank you very much for the reply!
So I just have to attach this to my player and select the layer, the ray should ignore?