- Home /
error CS0019: Operator `<=' cannot be applied to operands of type `UnityEngine.Vector3' and `float'
Hello, I was trying to make an enemy do an attack animation when they got close enough to the player, but I have gotten caught into a snag. When using comparison operators to compare Vector3 and float values, its not going to work out is what I found out. I tried looking in the documentation but I feel like I'm going in there blindly and don't know where to start really to solve this. I would really appreciate any help from people and take advises they would give.
here is my code that I was working on that started causing me trouble:
using UnityEngine;
using System.Collections;
public class EnemyAIMelee : MonoBehaviour
{
public float targetDistance = 20.0f;
public float attackDistance = 10.0f;
public float enemySpeed = -40.0f;
public float chaseSpeed = 0.0f;
private bool spawningIn = true;
private bool unlockAI = false;
public GameObject playerCharacterManager;
void Start()
{
playerCharacterManager = GameObject.FindGameObjectWithTag ("PlayerCharacterManager");
}
// Update is called once per frame
void LateUpdate ()
{
SpawnIn ();
MovingAI ();
AttackAI ();
}
void SpawnIn()
{
if (spawningIn == true)
{
GameObject plane = GameObject.FindGameObjectWithTag("PlayerPlane");
if (transform.position.z - plane.transform.position.z <= targetDistance)
{
SendMessage("EnemyMovingAnimation", false);
transform.parent = plane.transform;
unlockAI=true;
spawningIn = false;
}
else
{
SendMessage("EnemyMovingAnimation", true);
transform.position += transform.forward*enemySpeed*Time.deltaTime;
}
}
}
void MovingAI()
{
if(unlockAI == true)
{
float chasingSpeed = chaseSpeed * Time.deltaTime;
//transform.LookAt(playerCharacterManager);
transform.position = Vector3.MoveTowards (transform.position, playerCharacterManager.transform.position, chasingSpeed);
SendMessage ("EnemyMovingAnimation", true);
//This IF STATEMENT IS THE PROBLEM
if(transform.position - playerCharacterManager.transform.position <= attackDistance)
{
AttackAI ();
}
}
}
void AttackAI()
{
SendMessage ("EnemyAttackAnimation", true);
}
}
Answer by zaid87 · Jan 30, 2015 at 04:52 AM
If you want to make the enemy attack when they're inside a certain distance, you can calculate the distance using Vector3.Distance(). Then you can just check if it's lower or equal to attackDistance and attack if it is. For more info:
http://docs.unity3d.com/ScriptReference/Vector3.Distance.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Use vim with unity? 1 Answer
How to get multiple string values from override ToString()? 1 Answer