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 /
This question was closed Mar 02, 2014 at 09:47 PM by Fattie for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by jordmax12 · Mar 02, 2014 at 08:36 PM · c#artificial intelligencefinite-state-machine

What's wrong with my Enemy script?

I am completely stumped, very few times in programming have I said that but I can't seem to figure out how to get my enemy to flee away from my player! I am new to FSM's and AI behaviors so that might be why, but I get a null reference when this runs, not sure why. I just want a straight forward answer what I am doing wrong, and how I can solve it. I have been trying to figure this out for past couple days-week now.

 using UnityEngine;
 using System.Collections;
 
 public class FleeScript : MonoBehaviour {
     PlayerController PC;
     public int nextStateClicker;//change to private
     public float FleeTime;
     public GameObject target;
     public Transform thisTransform; //thisTransform
     public float minDistance = 10;
     public float runSpeed = 10;
 
     public float maxVel;
     // Use this for initialization
     void Start () 
     {
         Debug.Log ("Player has obtained coin, flee mode!");
         ChangeStateFlee ();
         PC = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
         maxVel = 28;
     }
 
     void Awake()
     {
         thisTransform = transform;
         thisTransform.position = new Vector3 (2, 0, 2);
     }
     // Update is called once per frame
     void Update () {
         
     }
 
     public enum FleeState
     {
         FSearch,
         FFlee,
     }
 
     public FleeState fleeState;
 
     IEnumerator FSearchState()
     {
         fleeState = FleeState.FFlee;
         yield return 0;
 
     }
 
     IEnumerator FFleeState()
     {
         /*Vector3 distance = targetTrans.position - transform.position;
         float updatesAhead = distance.magnitude / maxVel;
         Vector3 movePosition = target.transform.position + target.transform.rigidbody.velocity * updatesAhead;
         yield return (movePosition);*/
 
   

             if(Vector3.Distance(PC.GetTransform().position, thisTransform.position) < minDistance)
             {
                 Vector3 direction = thisTransform.position - PC.GetTransform ().position;
                 direction.Normalize();
                 thisTransform.position = Vector3.MoveTowards (thisTransform.position, direction * minDistance, Time.deltaTime * runSpeed);
             }
 
         yield return 0;
     }
 
     /*IEnumerator FleeTimer()
     {
 
     } Set Later */
 
     void ChangeStateFlee()
     {
         StartCoroutine(FSearchState ());
     }
 
     void ChangeStateSearch()
     {
         StartCoroutine(FFleeState());
     }
 }

Errors: NullReferenceExcetion get_position () and brings me to Vector3.Distance(PC... line

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 getyour411 · Mar 02, 2014 at 08:51 PM 0
Share

For this and future posts, when asking for help with an error, please be specific about the line#.

Have you tried PC.transform.position?

1 Reply

  • Sort: 
avatar image
0

Answer by indiebuilder · Mar 02, 2014 at 09:47 PM

Well I am also new to AI behaviors, found two script on answers one for C# and one for JavaScript.

C#
http://answers.unity3d.com/questions/26177/how-to-create-a-basic-follow-ai.html

JAVA
http://answers.unity3d.com/questions/475930/how-to-create-an-ai-follow-script-in-java.html

Hope this helped do I didn't give you a straight forward answer . Good luck -indie

Comment
Add comment · Show 1 · 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 jordmax12 · Mar 02, 2014 at 10:05 PM 0
Share

This would be awesome if it was for fleeing, I already know how to make it follow :) thanks though.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it good practice to implement FSM for player movements(FPS)? 0 Answers

Check lenght from player? 2 Answers

How to go about storing each trading card's effects/powers in tables for AI to use? 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