Hello, I have a code which I currently test it. The code is in javascript. I’ve convert the code to C# but when I test the code, it did nothing. Am I missing something in the code? The code returns no error though. Can somebody lend me a hand to solve this out? By the way, the code is not mine, just using it for reference purpose.
Here’s the javascript version:
#pragma strict
var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
function Start(){
character = GetComponent(CharacterController);
}
function Update(){
if(currentWaypoint < waypoint.length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (curTime == 0)
curTime = Time.time; // Pause over the Waypoint
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
and here’s the C# version:
using UnityEngine;
using System.Collections;
public class ClassName: MonoBehaviour {
public Transform[] waypoint; // The amount of Waypoint you want
public float patrolSpeed = 3f; // The walking speed between Waypoints
public bool loop = true; // Do you want to keep repeating the Waypoints
public float dampingLook= 6.0f; // How slowly to turn
public float pauseDuration = 0; // How long to pause at a Waypoint
private float curTime;
private int currentWaypoint = 0;
private CharacterController character;
void Start (){
character = GetComponent<CharacterController>();
}
void Update (){
if(currentWaypoint < waypoint.length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
void patrol (){
Vector3 target = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
Vector3 moveDirection = target - transform.position;
if(moveDirection.magnitude < 0.5f){
if (curTime == 0)
curTime = Time.time; // Pause over the Waypoint
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
var rotation= Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
}