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

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