- Home /
Code not working in C#
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);
}
}
}
Answer by ahaykal · Jun 18, 2013 at 09:20 AM
what is the name of the script? If it is not "ClassName" then this is your problem. The name of the script should match the name of the class.
The name of the script is circling. I just put it for example purposes. What this code does it makes the character traverse in a series of waypoints then returns to the original starting point, then continues to travese the waypoints again.
What he means is that the name of the class must match the filename for it to function at all.
I thought that would give an error though?
Your answer
Follow this Question
Related Questions
c# to JavaScript how? 3 Answers
Multiple Cars not working 1 Answer
Cant get C# to talk to JavaScript 3 Answers
Translating js to c#, getting NULLReferenceExeption 2 Answers
Distribute terrain in zones 3 Answers