Question by
Cameron Murtagh · Mar 03, 2016 at 02:02 PM ·
convertjava to c#
Can someone help me please?
Follow Player when in sight script!
Hello everyone I have this javascript...
import UnityEngine.SceneManagement;
public var fieldOfViewAngle : float = 150f; // Number of degrees, centred on forward, for the enemy see.
public var playerInSight : boolean; // Whether or not the player is currently sighted.
private var nav : NavMeshAgent; // Reference to the NavMeshAgent component.
private var player : GameObject; // Reference to the player.
function Awake ()
{
// Setting up the references.
nav = GetComponent(NavMeshAgent);
player = GameObject.FindGameObjectWithTag("Player");
playerInSight = false;
}
function Update ()
{
if (playerInSight)
{
nav.SetDestination(player.transform.position);
}
}
function OnTriggerStay (other : Collider)
{
// If the player has entered the trigger sphere...
if(other.gameObject.tag == "Player")
{
// By default the player is not in sight.
// Create a vector from the enemy to the player and store the angle between it and forward.
var direction : Vector3 = other.transform.position - transform.position;
var angle : float = Vector3.Angle(direction, transform.forward);
// If the angle between forward and where the player is, is less than half the angle of view...
if(angle < fieldOfViewAngle * 0.5f)
{
var hit : RaycastHit;
//Debug.Log("intrigger");
// ... and if a raycast towards the player hits something...
if(Physics.Linecast(transform.position , other.transform.position)!=true)
{
// ... the player is in sight.
playerInSight = true;
if (Vector3.Distance(transform.position , other.transform.position)<=1.5)
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
}
}
}
function OnTriggerExit (other : Collider)
{
// If the player leaves the trigger zone...
if(other.GetComponent.<Collider>().gameObject.tag == "Player")
// ... the player is not in sight.
Invoke("LooseSightDelayed",1);
}
function LooseSightDelayed()
{
playerInSight = false;
}
/* function CalculatePathLength (targetPosition : Vector3)
{
// Create a path and set it based on a target position.
var path : NavMeshPath = new NavMeshPath();
if(nav.enabled)
nav.CalculatePath(targetPosition, path);
Debug.Log("triggered");
// Create an array of points which is the length of the number of corners in the path + 2.
var allWayPoints : Vector3[] = new Vector3[path.corners.Length + 2];
// The first point is the enemy's position.
allWayPoints[0] = transform.position;
// The last point is the target position.
allWayPoints[allWayPoints.Length - 1] = targetPosition;
// The points inbetween are the corners of the path.
for(var i = 0; i < path.corners.Length; i++)
{
allWayPoints[i + 1] = path.corners[i];
}
// Create a float to store the path length that is by default 0.
var pathLength : float = 0;
// Increment the path length by an amount equal to the distance between each waypoint and the next.
for(var j = 0; j < allWayPoints.Length - 1; j++)
{
pathLength += Vector3.Distance(allWayPoints[j], allWayPoints[j + 1]);
}
return pathLength;
}*/
and I am not too experienced with this particular type of script but I got help and made it work! I am making a game using c# and want all the scripts to be the same type so I was wondering is there anyone out there that would take the time to convert this script into c# for me please and thank you! I would really appreciate it!!!
-Cameron-
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612070430im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Convert Java to C# 2 Answers
change my code from java to c# 1 Answer
JavaScript to C# 1 Answer
Heightmap From Texture - Script Converter 2 Answers
Convert Java to C# 1 Answer