- Home /
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);
}
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"))); } }
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!
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.
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/
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
![](https://koobas.hobune.stream/wayback/20220613080648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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