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 YesKlam · Dec 23, 2012 at 07:25 PM · c#aienemyconversioncs

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
Add comment · Show 1
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 rodr · Dec 29, 2012 at 10:20 PM 0
Share

Really hard to really help out because of the formulation of the question. I'm sorry.

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

Send an enemy back to its spawn point using waypoints 2 Answers

My bullet has no velocity after it is spawned. 1 Answer

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

Why doesn't this AI script work? c# 1 Answer

Multiple Cars not working 1 Answer


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