- Home /
HELP. Camera Movement vs. NPC detection.
For a school assignment we've been asked to redesign the Stealth map using the knowledge we've learned from watching a few tutorial videos. (Mind me, that is hardly any knowledge at all, so forgive me in advance).
Okay. After recreating the Stealth tutorial perfectly, I changed around the map using different models and walls from the asset store. After recreating this new map, I created a new nav-mesh so that the NPC's would have somewhere to go and patrol, but here is where the problem comes in.
I can either: a. Change the layer of all my walls and objects to "Ignore Raycast" so that my camera does not stop when it detects a model, and watches my player move into the distance, slowly becoming microscopic. or b. Keep it enabled, because when ray cast is ignored, my robot guard NPC's can detect my movement through walls, and proceed to shoot me through them also.
This is why I've come to ask you guys. I've got the amount of knowledge that is sufficient to move a few walls and lights around, and maybe make a few new components and meshes, but I have no idea on how to re-code the camera movement script to use a different method of obstacle direction. (Preferably, I don't care if the camera detects an obstacle, I just want it to pan around above my player, following where he goes).
All In all, is there a way to recode the camera movement script so that I don't need to sacrifice letting NPC's see me and shoot me through my objects that are ignoring ray-cast? If so, can anyone fix it for me?
Thank-you in advance for any work you put into this, as I'm sure everyone has something better to do and it would be a great sacrifice of time.
I will post my Camera Movement script below, and feel free to make any changes to it you find necessary.
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour
{
public float smooth = 1.5f;
private Transform player;
private Vector3 relCameraPos;
private float relCameraPosMag;
private Vector3 newPos;
void Awake ()
{
player = GameObject.FindGameObjectWithTag(Tags.player).transform;
relCameraPos = transform.position - player.position;
relCameraPosMag = relCameraPos.magnitude - 0.5f;
}
void FixedUpdate ()
{
Vector3 standardPos = player.position + relCameraPos;
Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
Vector3[] checkPoints = new Vector3[5];
checkPoints[0] = standardPos;
checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
checkPoints[4] = abovePos;
for(int i = 0; i < checkPoints.Length; i++)
{
if(ViewingPosCheck(checkPoints[i]))
break;
}
transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
SmoothLookAt();
}
bool ViewingPosCheck (Vector3 checkPos)
{
RaycastHit hit;
if(Physics.Raycast(checkPos, player.position - checkPos, out hit, relCameraPosMag))
if(hit.transform != player)
return false;
newPos = checkPos;
return true;
}
void SmoothLookAt ()
{
Vector3 relPlayerPosition = player.position - transform.position;
Quaternion lookAtRotation = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
}
}
It's doubtful anyone with the know-how to help has any experience with the S$$anonymous$$lth demo - I'd never heard of it until just now.
Why would the camera need to stop upon detecting a model with a raycast? In what way is that useful?
Can you not just mimic whatever relevant settings the tutorial used for these objects you've replaced?
I'm not sure. The camera has a style, and when I say that I mean that it doesn't just act like a normal camera, but when you enter a building it sort of changes are rotates for where you go inside it. But the camera has nothing to do with the objects rather than using Raycast.
You can give raycast a custom layermask, so that it will only touch colliders on the layer(s) you're interested in. Your camera can cast against a layermask that skips walls, while your robots cast against a layermask that includes them.
This might involve creating a new "wall" layer.