Hello Guys,
I am finding myself needing to modify a script that I found in an old tutorial the original script was in JS and apart from one small error (line 65) in the script that I can’t seem to fix it’s working and also the fact that JS is going to be retired soon so I need a newer version. This has been my go to script whenever I need to move a NPC.
Anyway, I have been trying to modify the script to CS and I keep running into errors and going batty reading the Unity docs and basically going nowhere. The script is quite long.
So, if anyone can help me modify it to get it working I would be most grateful. I realize that doing the whole script would be a burden on anyone, so, perhaps it would be better if I ask for specific help on the actual lines of code that I don’t understand.
This script is as follows:
#pragma strict
var player: Transform;
var distanceToRobot: float;
var anime: Animator;
var theBool: boolean;
var agent: UnityEngine.AI.NavMeshAgent;
// where we want our robot to move to
var wayPoints: Transform[];
var wayPointIndex: int = 0;
var currentSpeed: float;
var angularSpeed: float;
var angleResponseTime: float = 0.6f;
var angularSpeedDampingTime: float = 0.7f;
var angle: float;
public var pathEndThreshold: float = 0.1f;
private var hasPath: boolean = false;
function atEndOfPath(){
//print("atEndOfPath");
if (Vector3.Distance(transform.position, agent.destination) <= 1f){
return true;
}
return false;//*/
/*hasPath |= agent.hasPath;
if (hasPath agent.remainingDistance <= agent.stoppingDistance + pathEndThreshold ){
// Arrived
hasPath = false;
return true;
}
return false;//*/
}
function Start () {
anime = GetComponent(Animator);
agent = GetComponent(UnityEngine.AI.NavMeshAgent);
anime.SetLayerWeight(1, 1f);
//this causes a problem I can't fix
//anime.SetLayerWeight(2, 1f);
theBool = false;
}
function Update () {
distanceToRobot = Vector3.Distance(player.position, transform.position);
//print("Distance to Robot: "+distanceToRobot);
//seems to be 2 ways of doing the destination thingy
//and both seem to work
//this is the first method I saw
//navMesh.SetDestination(wayPoints[wayPointIndex].position);
//this is the 2nd
agent.destination = wayPoints[wayPointIndex].position;
currentSpeed = Vector3.Project(agent.desiredVelocity,transform.forward).magnitude;
//print("currentSpeed: "+currentSpeed);
//currentSpeed = .5f;
//if PlayerInSight
var getVal: boolean;
getVal = anime.GetBool("PlayerInSight");
//print("getVal: " + getVal);
if(getVal==true){
agent.Stop();
angle = FindAngle(transform.forward, player.position - transform.position, transform.up);
//currentSpeed = 0f;
var rotation = Quaternion.LookRotation(player.position - transform.position );
transform.rotation = Quaternion.Slerp(transform.rotation,rotation, Time.deltaTime * 4);
angularSpeed = angle / angleResponseTime;
//print("currentSpeed: "+currentSpeed);
}
else{
agent.Resume();
angle = FindAngle(transform.forward, agent.desiredVelocity, transform.up);
angularSpeed = angle / angleResponseTime;
currentSpeed = Vector3.Project(agent.desiredVelocity,transform.forward).magnitude;
}
//it's a function - remember the "()" to call it not a variable
//makes you wonder why it it called it in the first place
if (atEndOfPath() == true){
print("At end of Path! Give me somewhere to go you stupid carbon based life form!!!");
if(wayPointIndex == wayPoints.Length-1){
wayPointIndex = 0;
}
else{
wayPointIndex += 1;
}//end if
}//end if
print("wayPointIndex:"+wayPointIndex);
//how to make robot walk to waypoint
//instead of slide
anime.SetFloat("Speed", currentSpeed);
//anime.SetFloat("AngularSpeed",0f);
anime.SetFloat("AngularSpeed", angularSpeed, angularSpeedDampingTime, Time.deltaTime);
//print("Distance to Robot: "+distanceToRobot);
if(distanceToRobot>15){
theBool = false;
anime.SetBool("PlayerInSight", theBool);
}
//print("Distance to Robot: "+distanceToRobot);
if(distanceToRobot<15){
theBool = true;
anime.SetBool("PlayerInSight", theBool);
}
}//end update
//A function that finds the correct angle we should be facing
//this is beyond my ability to comprehend
function FindAngle(fromVector : Vector3, toVector:Vector3, upVector:Vector3){
//do something
//print("FindAngle......................");
//return 3;
if(toVector == Vector3.zero){
return 0f;
}
var myAngle: float;
var normal: Vector3;
myAngle = Vector3.Angle(fromVector, toVector);
normal = Vector3.Cross( fromVector, toVector);
myAngle *= Mathf.Sign(Vector3.Dot(normal, upVector));
myAngle *= Mathf.Deg2Rad;
return myAngle;
}//end findangle()
Like I said, it is long and I only have the atEndofPath and Start functions done after that the code gets a bit funky .
If you are willing to help, here is a huge thankyou in advance**!.**
Regards.