• 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
0
Question by AntLewis · Sep 05, 2012 at 03:16 PM · worldspacelocalspace

WorldSpace vs LocalSpace problem

Hi, I have a section of code which detects whether an object is within another objects viewcone. This works as anticipated (grabbed from other areas of the forum), however, it always forward in world space, and I'd like to alter it so it's alway looks forward in local space (so if the character the script is attached to turns, the viewcone turns with the way he is moving). I've been struggling with it and can't seem to get the results I'm after. Could anyone advise me what to change in order to get this working? I pass the transform of an object to CheckCanSee, this then checks the angle (and also draws the viewcone, which again is locked to world space). Thanks for any pointers

     public bool CheckCanSee (Transform canISeeThis)
     {
         if (drawViewcone) {
             // Draws the viewcone
             DrawLine (distanceThreshold, fieldOfView/2.0f, 0.0f); 
             DrawLine (distanceThreshold, (-fieldOfView)/2.0f, 0.0f); 
         }
     
         Vector3 direction = canISeeThis.position - transform.position;
  
         // Calculate angle to object
         angleToObject = Vector3.Angle (direction, transform.forward);
         Debug.Log ("Object is " + angleToObject + " degrees.");
   
         if (angleToObject > ( fieldOfView * 0.5f ) ) {
             // The target is outside the field of view!
             Debug.DrawLine (transform.position, canISeeThis.position, new Color (1.0f, 0.0f, 0.0f, 1.0f), 0.5f);
             Debug.Log ("Outside FOV");
             return false;
         }
  
         Debug.DrawLine (transform.position, canISeeThis.position, new Color (0.0f, 1.0f, 0.0f, 1.0f), 0.5f);
  
         return true;
     }
     
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     // Draws a line, given specific angles    
     private void DrawLine (float distance, float xAngle, float yAngle)
     {      
  
         float x =  transform.position.x + distance * Mathf.Sin (xAngle * Mathf.Deg2Rad) * Mathf.Cos (yAngle * Mathf.Deg2Rad);
         float y = transform.position.y + distance * Mathf.Sin (xAngle * Mathf.Deg2Rad) * Mathf.Sin (yAngle * Mathf.Deg2Rad);
         float z = transform.position.z + distance * Mathf.Cos (xAngle * Mathf.Deg2Rad);
 
         Vector3 finalPos = new Vector3 (x, y, z);            
 
         Debug.DrawLine (transform.position, finalPos, new Color (0.0f, 0.0f, 1.0f, 0.3f), 0.8f);
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Sep 05, 2012 at 07:09 PM

as i understood your question, you just need a check if object is in view cone?

using UnityEngine; using System.Collections; public class ViewConeIn : MonoBehaviour { public Transform ObjectToViewCheck; public float maxDistance = 10f; public float maxAngle = 60f; void OnGUI() { float distance = Vector3.Distance(transform.position, ObjectToViewCheck.position); float angle = Vector3.Angle(transform.forward, ObjectToViewCheck.position - transform.position); GUI.color = distance > maxDistance ? Color.red : Color.green; GUI.Box(new Rect(5, 5, 100, 30), string.Format("distance: {0}", distance.ToString("0.0"))); GUI.color = angle > maxAngle ? Color.red : Color.green; GUI.Box(new Rect(5, 40, 100, 30), string.Format("angle: {0}", angle.ToString("0.0"))); } }

Comment
Add comment · Show 4 · Share
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 AntLewis · Sep 05, 2012 at 07:25 PM 0
Share

No sorry I think you've misunderstood. The viewcone always points forward in world space, but I want it to be forward in local space (so it faces teh direction that the characer is moving). Thanks!

avatar image ScroodgeM · Sep 05, 2012 at 07:27 PM 0
Share

i'd use transform.forward here in my script, it is local's forward direction translated to world space.

attach script to some object, create another object and reference second object to this script as object to look at. then run the game and move/rotate objects.

avatar image AntLewis · Sep 05, 2012 at 08:12 PM 0
Share

Ah I see - can I ask, if the target object is directly in front of the object that the script is attached to, it reduces to around 4 or 5 degrees then increases again - should it be hitting '0' degrees? Thanks again for the help/

avatar image ScroodgeM · Sep 06, 2012 at 04:34 AM 0
Share

if object directly in front of the object you will get 0 degrees, but the object in your case me thinks a little up or down from looking object and moving it in horizontal axis will not make 0 degrees. be sure that both vertical and horizontal relative position is close to zero to get 0 degrees

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DragRigidbody - move object in Local Space, not World Space 1 Answer

Clamping rotation doesn't work when local axis is different from global 0 Answers

How do I make an object move on the world space rather than the local space? 1 Answer

Is it possible to convert Mesh Vertices Local Position to World Space? 0 Answers

How to use Transform.TransformDirection of another transform to affect a separate Transforms local rotation? 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