- Home /
Enemy AI C#
I'm trying to translate AI code from JS to C#
 public void FindPath (Vector3 Destinazione){
         if (!enabled)
             return;
             
         Vector3 DestinazioneProj;
         
             DestinazioneProj = GridProjection(Destinazione, 50.0f);
             SendMessage("TargetPosition", DestinazioneProj, SendMessageOptions.DontRequireReceiver);
             MoveTowards(WaypointAttuale);
     }
     
     public void CurrentWaypoint ( Vector3 Posizione  ){
         WaypointAttuale = Posizione;
     }
     
     public IEnumerator Pattuglia (){
         Debug.Log("Pattuglia");
         AutoWayPoint WaypointAutomatico = AutoWayPoint.FindClosest(t.position, WaypointNomeGruppo);
         
         while (true)
         {
             if (Bersaglio == null)
                 NemicoRilevato = false;
                 
             if (FindClosest() != null)
                 Bersaglio = FindClosest().transform;
                 
             
             if (!Pericolo && CanSeeTarget(Bersaglio, AttaccoRaggio))
             {
                 if (RilevaSuono.Length!=0 && Time.time > RilevaTimer)
                 {
                     RilevaTimer = Time.time + Random.Range(0.75f, 1.25f) * RilevaDelta;
                 }
                 
                 if (SparaNemico)
                      StartCoroutine("Attacco");
             }
             
             if (ControllerVisuale != null)
             {
                 ControllerVisuale.targetTransform = Bersaglio;
                 for (int i=0; i < ControllerVisuale.segments.Length; i++)
                 {
                     ControllerVisuale.segments[i].effect = Mathf.Lerp(ControllerVisuale.segments[i].effect, 0, Time.deltaTime * 3.0f);
                 }
             }
             
             Vector3 WaypointPosizione;
             
             if (WaypointAutomatico == null)
                 WaypointPosizione = PosizioneIniziale;
                 
             else
             {
                 WaypointPosizione = WaypointAutomatico.transform.position;
                 PattugliaPausaTempo = WaypointAutomatico.pauseTime;
             }
             // Are we close to a waypoint? -> pick the next one!
             if (Vector3.Distance(WaypointPosizione, t.position) < NextWaypointDistanza)
             {
                 if (WaypointAutomatico != null)
                     WaypointAutomatico = ScegliNextWaypoint(WaypointAutomatico);
                 StartCoroutine("Idle");
             }
             
             
             FindPath(WaypointPosizione);
             
             yield return 0;
         }
     }
     
     public IEnumerator SearchPlayer ( Vector3 Posizione  ){
         Debug.Log("SearchPlayer");
         // Run towards the player but after 3 seconds timeout and go back to Patroling
         float Timeout = Random.Range(CercaTempoMin, CercaTempoMax);
         float CercaNonIntuitivaTempo = Timeout - Random.Range(0.8f, 1.25f) * CercaIntuitivaTempo;
         while (Timeout > 0.0f) {
             if (Pericolo)
                 yield return 0;
                 
             // Target is dead - stop hunting
             if (Bersaglio == null)
                 yield return 0;
                 
             if (FindClosest() != null)
                 Bersaglio = FindClosest().transform;
                 
             // We found the player
             if (CanSeeTarget (Bersaglio, AttaccoRaggio))
                 yield return 0;
                 
             if (Timeout > CercaNonIntuitivaTempo)
                 Posizione = Bersaglio.position;
                 
             if (ControllerVisuale != null)
             {
                 ControllerVisuale.targetTransform = Bersaglio;
                 for (int i=0; i<ControllerVisuale.segments.Length; i++)
                 {
                     ControllerVisuale.segments[i].effect = Mathf.Lerp(ControllerVisuale.segments[i].effect, 0, Time.deltaTime * 3.0f);
                 }
             }
             
             if (CercaSuono.Length!=0 && Time.time > CercaTimer)
             {
                 CercaTimer = Time.time + Random.Range(0.75f, 1.25f) * CercaDelta;
             }
             
             
             FindPath(Posizione);
             
             Timeout -= Time.deltaTime;
             yield return 0;
         }
     }
     
     public void RotateTowards (Vector3 Posizione){
         Debug.Log("RotateTowards");
         Movimento.SetSpeed(0.0f);
         
         Vector3 Direzione = Posizione - t.position;
         Direzione.y = 0;
     //    if (direction.magnitude < 0.1f)
     //        return;
             
         Movimento.RotateTowards(Direzione);
     }
     
     public void MoveTowards ( Vector3 Posizione  ){
         Debug.Log("MoveTowards");
         Vector3 Direzione = Posizione - t.position;
         Direzione.y = 0;
     //    if (direction.magnitude < 0.5f) {
     //        Movimento.SetSpeed(0.0f);
     //        return;
     //    }
         
         Movimento.MoveTowards(Direzione);
     }
     
     public void MoveBackwards ( Vector3 Posizione  ){
         Debug.Log("MoveBackwards");
         Vector3 Direzione = Posizione - t.position;
         Direzione.y = 0;
     //    if (direction.magnitude > 2.0f) {
     //        Movimento.SetSpeed(0.0f);
     //        return;
     //    }
         
         Movimento.MoveBackwards(Direzione);
     }
     
     AutoWayPoint ScegliNextWaypoint (AutoWayPoint WaypointAttuale){
         Debug.Log("ScegliNextWaypoint");
         // We want to find the waypoint where the character has to turn the least
         
         // The direction in which we are walking
         Vector3 forward = t.TransformDirection(Vector3.forward);
         
         // The closer two vectors, the larger the dot product will be.
         AutoWayPoint best = WaypointAttuale;
         float bestDot = -10.0f;
         foreach(AutoWayPoint Attuale in WaypointAttuale.connected)
         {
             Vector3 Direzione = Vector3.Normalize(Attuale.transform.position - t.position);
             float dot = Vector3.Dot(Direzione, forward);
             if (dot > bestDot && Attuale != WaypointAttuale)
             {
                 bestDot = dot;
                 best = Attuale;
             }
         }
         return best;
     }
When I run the game works, but nothing happen and I receive this error
 NullReferenceException
 UnityEngine.Transform.TransformDirection (Vector3 direction) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:193)
 AI_Movimento.MoveTowards (Vector3 direction) (at Assets/Standard Assets/AI_Movimento.js:96)
 AI.MoveTowards (Vector3 Posizione) (at Assets/Progetto/Personaggio/AI.cs:444)
 AI.FindPath (Vector3 Destinazione) (at Assets/Progetto/Personaggio/AI.cs:107)
This is the code of MoveTowards
 function MoveTowards (direction : Vector3) {
     // Rotate towards the target
     DirezioneVista = direction;
     if (DirezioneVista.magnitude>1) DirezioneVista = DirezioneVista.normalized;
     
     // Modify speed so we slow down when we are not facing the target
     if (Movimenti.MinMovimentoSpeed > 0) {
Line 96 --> var forward : Vector3 = t.TransformDirection(Vector3.forward); var speedModifier : float = Vector3.Dot(forward, direction.normalized); speedModifier = Mathf.Clamp01(speedModifier); cm.movement.maxForwardSpeed *= speedModifier; }
     var directionVector : Vector3 = direction.forward;
     if (directionVector.magnitude>1) directionVector = directionVector.normalized;
     if (cm.movement.maxForwardSpeed < Movimenti.MinMovimentoSpeed)
         cm.inputMoveDirection = Vector3.zero;
     else
         cm.inputMoveDirection = t.TransformDirection(directionVector);
 }
I hope someone helps me
               Comment
              
 
               
              Really hard to really help out because of the formulation of the question. I'm sorry.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                