Search Unity

JS to c# help

Discussion in 'Scripting' started by pauloaguiar, Apr 24, 2018.

  1. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Code (JavaScript):
    1. // The start waypoint, this is initialized in Awake.
    2. // This variable is static thus all instances of the waypoint script share it.
    3. static var start : WayPoint;
    4.  
    5. // The next waypoint, this variable needs to be assigned in the inspector.
    6. // You can select all waypoints to see the full waypoint path.
    7. var next : WayPoint;
    8.  
    9. // This is used to determine where the start waypoint is.
    10. var isStart = false;
    11.  
    12. // Returns where the AI should drive towards.
    13. // position is the current position of the car.
    14. function CalculateTargetPosition (position : Vector3) {
    15.  
    16.     // If we are getting close to the waypoint, we return the next waypoint.
    17.     // This gives us better car behaviour when cars don't exactly hit the waypoint
    18.     if (Vector3.Distance (transform.position, position) == 0) {
    19.         return next.transform.position;
    20.     }
    21.     // We are still far away from the next waypoint, just return the waypoints position
    22.     else {
    23.         return transform.position;
    24.     }
    25. }
    26.  
    27. // This initializes the start and goal static variables.
    28. // We have to do this inside Awake because the waypoints need
    29. // to be initialized before the AI scripts use it.
    30. // All Awake function are always called before all Start functions.
    31. function Awake () {
    32.     if (!next)
    33.         Debug.Log ("This waypoint is not connected, you need to set the next waypoint!", this);
    34.        
    35.     if (isStart)
    36.         start = this;
    37. }
    38.  
    39. // Draw the waypoint pickable gizmo
    40. function OnDrawGizmos () {
    41.     Gizmos.DrawIcon (transform.position, "Waypoint.tif");
    42. }
    43.  
    44. // Draw the waypoint lines only when you select one of the waypoints
    45. function OnDrawGizmosSelected () {
    46.     if (next) {
    47.         Gizmos.color = Color.green;
    48.         Gizmos.DrawLine (transform.position, next.transform.position);
    49.     }
    50. }

    so far i get error because return method

    c#
    Code (CSharp):
    1. static lapTrigger start;
    2.  
    3.     public lapTrigger next;
    4.  
    5.     public bool isAstart = false;
    6.  
    7.     void CalculateTargetPosition (Vector3 position) {
    8.  
    9.         if (Vector3.Distance (transform.position, transform.position) == 0) {
    10.  
    11.             return next.transform.position;
    12.         }
    13.         else
    14.         {
    15.             return transform.position;
    16.         }
    17.     }
    18.     // Use this for initialization
    19.     void Awake () {
    20.  
    21.         if (!next)
    22.             Debug.Log ("This waypoint is not connected, you need to set the next waypoint!", this);
    23.         if (isAstart)
    24.             start = this;
    25.     }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    UNTESTED

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class WayPoint : MonoBehaviour
    4. {
    5.  
    6.     // The start waypoint, this is initialized in Awake.
    7.     // This variable is static thus all instances of the waypoint script share it.
    8.     static WayPoint start;
    9.  
    10.     // The next waypoint, this variable needs to be assigned in the inspector.
    11.     // You can select all waypoints to see the full waypoint path.
    12.     WayPoint next;
    13.  
    14.     // This is used to determine where the start waypoint is.
    15.     bool isStart = false;
    16.  
    17.     // Returns where the AI should drive towards.
    18.     // position is the current position of the car.
    19.     Vector3 CalculateTargetPosition(Vector3 position)
    20.     {
    21.         // If we are getting close to the waypoint, we return the next waypoint.
    22.         // This gives us better car behaviour when cars don't exactly hit the waypoint
    23.         if (Vector3.Distance(transform.position, position) == 0)
    24.         {
    25.             return next.transform.position;
    26.         }
    27.         // We are still far away from the next waypoint, just return the waypoints position
    28.         else
    29.         {
    30.             return transform.position;
    31.         }
    32.     }
    33.  
    34.     // This initializes the start and goal static variables.
    35.     // We have to do this inside Awake because the waypoints need
    36.     // to be initialized before the AI scripts use it.
    37.     // All Awake function are always called before all Start functions.
    38.     void Awake()
    39.     {
    40.         if (!next)
    41.             Debug.Log("This waypoint is not connected, you need to set the next waypoint!", this);
    42.  
    43.         if (isStart)
    44.             start = this;
    45.     }
    46.  
    47.     // Draw the waypoint pickable gizmo
    48.     void OnDrawGizmos()
    49.     {
    50.         Gizmos.DrawIcon(transform.position, "Waypoint.tif");
    51.     }
    52.  
    53.     // Draw the waypoint lines only when you select one of the waypoints
    54.     void OnDrawGizmosSelected()
    55.     {
    56.         if (next)
    57.         {
    58.             Gizmos.color = Color.green;
    59.             Gizmos.DrawLine(transform.position, next.transform.position);
    60.         }
    61.     }
    62. }
     
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I already did this using the converter. It gives the same errors.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I did that by hand.

    EDIT: You needed to change "void" to "Vector3" was the problem you were having FYI...
     
  5. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    i dont see that tks :)
     
  6. AdrianoVerona_Unity

    AdrianoVerona_Unity

    Unity Technologies

    Joined:
    Apr 11, 2013
    Posts:
    317
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700