• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by DCoulstock · Aug 25, 2014 at 04:31 AM · cameratutorialdetectionstealth

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);
     }
 }
 
Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DCoulstock · Aug 26, 2014 at 01:14 AM 0
Share

Any answers would be highly appreciated!

avatar image DCoulstock · Aug 27, 2014 at 10:59 PM 0
Share

REALLY NEED HELP GUYS!!

avatar image AlwaysSunny · Aug 28, 2014 at 01:02 AM 0
Share

It's doubtful anyone with the know-how to help has any experience with the Stealth 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?

avatar image DCoulstock · Sep 03, 2014 at 10:57 PM 0
Share

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.

avatar image rutter · Sep 03, 2014 at 11:02 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

raycast hit not detecting && Nav mesh agent ai stealth 1 Answer

Unity wont detect device camera 0 Answers

2D Gameplay Tutorial - Main Camera 1 Answer

Why does my navmesh not stop at this wall? (stealth tutorial) 1 Answer

Check if is object in camera field and not covered by other object 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges