- Home /
 
stupid errors i can't figure out
hey guys, i am trying to get this enemy AI for A* to work with my custom character controller, but i am getting a few stupid errors that i have tried several different ways to fix but i cannot get it.
the problem seems to be with the direction the player is supposed to be moving in.
here is the code
 using UnityEngine;
 using System.Collections;
 using Pathfinding;
 
 [RequireComponent (typeof (Rigidbody2D))]
 [RequireComponent (typeof (Seeker))]
 public class EnemyAI : MonoBehaviour
 {
 
     // What to chase?
     public Transform target;
     
     // How many times each second we will update our path
     public float updateRate = 2f;
 
     //enemy effect played on destruction
     public GameObject DestroyedEffect;
 
     // Caching
     private Seeker seeker;
     private Rigidbody2D rb;
     
     //The calculated path
     public Path path;
     
     //The AI's movement
     public float Speed;
     private float _normalizedDirection;
     private Vector2 _direction;
     private Vector2 _startPosition;
     private CharacterController2D _controller;
     
     [HideInInspector]
     public bool pathIsEnded = false;
     
     // The max distance from the AI to a waypoint for it to continue to the next waypoint
     public float nextWaypointDistance = 3;
     
     // The waypoint we are currently moving towards
     private int currentWaypoint = 0;
     
     void Start ()
     {
         _normalizedDirection = (path.vectorPath[currentWaypoint] - _startPosition).normalized;
         _controller = GetComponent<CharacterController2D> ();
         _startPosition = transform.position;
         _direction = new Vector2(_normalizedDirection, 0);
         seeker = GetComponent<Seeker>();
         rb = GetComponent<Rigidbody2D>();
         
         if (target == null) {
             Debug.LogError ("No Player found? PANIC!");
             return;
         }
         
         // Start a new path to the target position, return the result to the OnPathComplete method
         seeker.StartPath (transform.position, target.position, OnPathComplete);
         
         StartCoroutine (UpdatePath ());
     }
     
     IEnumerator UpdatePath () {
         if (target == null) {
             //TODO: Insert a player search here.
             return false;
         }
         
         // Start a new path to the target position, return the result to the OnPathComplete method
         seeker.StartPath (transform.position, target.position, OnPathComplete);
         
         yield return new WaitForSeconds ( 1f/updateRate );
         StartCoroutine (UpdatePath());
     }
     
     public void OnPathComplete (Path p) {
         Debug.Log ("We got a path. Did it have an error? " + p.error);
         if (!p.error) {
             path = p;
             currentWaypoint = 0;
         }
     }
     
     void FixedUpdate () {
         if (target == null) {
             //TODO: Insert a player search here.
             return;
         }
         
         //TODO: Always look at player?
         
         if (path == null)
             return;
         
         if (currentWaypoint >= path.vectorPath.Count) {
             if (pathIsEnded)
                 return;
             
             Debug.Log ("End of path reached.");
             pathIsEnded = true;
             return;
         }
         pathIsEnded = false;
     
         //Direction to the next waypoint
         //Vector3 dir = ( path.vectorPath[currentWaypoint] - transform.position ).normalized;
         
         //Move the AI
          _controller.SetHorizontalForce(_direction.x * Speed);
         
         float dist = Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]);
         if (dist < nextWaypointDistance) {
             currentWaypoint++;
             return;
         }
     }
     
 }
 
               here are the errors
 Assets/Scripts/EnemyAI.cs(44,46): error CS0121: The call is ambiguous between the following methods or properties: `UnityEngine.Vector2.operator -(UnityEngine.Vector2, UnityEngine.Vector2)' and `UnityEngine.Vector3.operator -(UnityEngine.Vector3, UnityEngine.Vector3)'
 
 Assets/Scripts/EnemyAI.cs(44,17): error CS0029: Cannot implicitly convert type `UnityEngine.Vector3' to `float'
 
              
               Comment
              
 
               
              Answer by tanoshimi · Jan 28, 2015 at 08:22 PM
 private float _normalizedDirection;
 
               How can "normalized direction" be a float? Did you mean:
 private Vector3 _normalizedDirection;
 
               that changes the errors to ones i had before
 
     Assets/Scripts/EnemyAI.cs(44,46): error CS0121: The call is ambiguous between the following methods or properties: `UnityEngine.Vector2.operator -(UnityEngine.Vector2, UnityEngine.Vector2)' and `UnityEngine.Vector3.operator -(UnityEngine.Vector3, UnityEngine.Vector3)'
     
     
     Assets/Scripts/EnemyAI.cs(47,66): error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments
     
     
     Assets/Scripts/EnemyAI.cs(47,66): error CS1503: Argument `#1' cannot convert `UnityEngine.Vector3' expression to type `float'
     
 
                 Your answer
 
             Follow this Question
Related Questions
Need help calling a variable from another C# script 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
In game log not working. 2 Answers
Argument out of range. 1 Answer