Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by acap170190 · Jun 18, 2013 at 09:08 AM · c#javascript

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);
         }  
     }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 18, 2013 at 09:35 AM 0
Share

that's true

avatar image acap170190 · Jun 20, 2013 at 08:15 AM 0
Share

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.

avatar image Tarlius · Jun 21, 2013 at 06:55 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges