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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ujean917 · Jan 30, 2015 at 04:43 AM · c#scriptingbasicscompare

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);
         }
     }


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · 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

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

19 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

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

Why is my ID value getting stuck here? 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