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 Caramelkisses · Mar 23, 2013 at 12:19 AM · 2derrorvector3sidescrollervector3.distance

Decrease players health depending on distance of enemy.

I'm making a 2D game in which your a small girl fighting of ghosts. I've written code that moves the ghosts toward the player using Vector3.Lerp which is working as I intended. I want to use Vector3.Distance to find how far away the enemies are away from my character, so far I've been able to get a value for the distance and that seems to be working. But I'm having problems with how to remove the players health or "Sanity" at a faster rate depending on how close the enemies are.

This is what I've come up with so far: #pragma strict var CharacterPos : Vector3; var EnemyPos : Vector3; static var Distance1 : Vector3; var SanityValue: int;

 function Update ()
 
 { 
         var Distance1 : Transform;
         var dist = Vector3.Distance((CharacterMover.playerPos), (EnemyMovement2.EnemyPos));
      print ("Distance to other: " + dist);
        
                SanityRemover();  
      print (SanityValue); 
 }
 
 function SanityRemover() {
 
     if ((Distance1 > 4) & (Distance1 < 5)) {
         SanityValue -= 1 * Time.deltaTime; 
 }
     else if ((Distance1 > 3) & (Distance1 < 4)) { 
         SanityValue -= 2 * Time.deltaTime;
 }
 
     else if ((Distance1 > 2) & (Distance1 < 3)){
         SanityValue -= 3 * Time.deltaTime;
 }
     else if ((Distance1 > 1) & (Distance1 < 2)) {
         SanityValue -= 4 * Time.deltaTime; 
 }
     else if ((Distance1 > 0) & (Distance1 < 1)) {
         SanityValue -= 5 * Time.deltaTime;
 }
     
 }    

I'm getting the following errors:

Operator '<' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'. Operator '>' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'.
Also, is the way I've coded the if statements the best way of doing it? Thanks in advance!
Comment
Add comment · Show 4
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 agamedesigner · Mar 23, 2013 at 12:57 AM 0
Share

First of all, you should use && ins$$anonymous$$d of just & for this.

Second, you're comparing a Transform to an integer-- you're comparing Distance1 ins$$anonymous$$d of the dist variable.

avatar image Caramelkisses · Mar 23, 2013 at 12:25 PM 0
Share

Okay, so I've changed what you said was incorrect, thanks for the help. :) Here's the updated code. #pragma strict var CharacterPos : Vector3; var EnemyPos : Vector3; static var Distance1 : Vector3; var SanityValue: int; private var dist : int;

 function LateUpdate ()
 
 { 
         var Distance1 : Transform;
         var dist = Vector3.Distance((Character$$anonymous$$over.playerPos), (Enemy$$anonymous$$ovement2.EnemyPos));
      //print ("Distance to other: " + dist);
            print (SanityValue); 
        
                SanityRemover();  
              
 }
 
 function SanityRemover() {
 
     if ((dist > 4) && (dist < 5)) {
         SanityValue -= 1 * Time.deltaTime; 
 }
     else if ((dist > 3) && (dist < 4)) { 
         SanityValue -= 2 * Time.deltaTime;
 }
     else if ((dist > 2) && (dist < 3)){
         SanityValue -= 3 * Time.deltaTime;
 }
     else if ((dist > 1) && (dist < 2)) {
         SanityValue -= 4 * Time.deltaTime; 
 }
     else if ((dist > 0) && (dist < 1)) {
         SanityValue -= 5 * Time.deltaTime;
 }    
 
 }    


But now I'm having other problems, the sanity value doesn't seem to be working at all. It just fluctuate between 0 and 100 constantly any idea's what may be causing this?

Thanks again.

avatar image Caramelkisses · Mar 25, 2013 at 12:09 AM 0
Share

Thank you so much! It works perfectly. I really appreciate it, I wouldn't have got close to making it work without you. :D

avatar image agamedesigner · Mar 25, 2013 at 02:09 AM 0
Share

No problem, I hope you were able to learn something from it^^

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by agamedesigner · Mar 24, 2013 at 08:04 PM

Try this:

 #pragma strict
 
 var SanityValue: float = 100;
 private var dist : float;
 
 function LateUpdate ()
 {
     SanityRemover();
 }
  
 function SanityRemover() {
     dist = Vector3.Distance((CharacterMover.playerPos), (EnemyMovement2.EnemyPos));
     print ("Distance: " + dist);
 
     if (CheckWithinRange(dist, 4, 5)) {
         SanityValue -= 1 * Time.deltaTime; 
     }
     else if (CheckWithinRange(dist, 3, 4)) { 
         SanityValue -= 2 * Time.deltaTime;
     }
     else if (CheckWithinRange(dist, 2, 3)){
         SanityValue -= 3 * Time.deltaTime;
     }
     else if (CheckWithinRange(dist, 1, 2)) {
         SanityValue -= 4 * Time.deltaTime; 
     }
     else if (CheckWithinRange(dist, 0, 1)) {
         SanityValue -= 5 * Time.deltaTime;
     }
 
     print ("Sanity: " + SanityValue);
 }
 
 function CheckWithinRange(givenDist : float, min : int, max : int)
 {
     if(givenDist > min && givenDist < max)
         return true;
     else
         return false;
 }
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

11 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

Related Questions

Making the players head face toward the location of the mouse? 1 Answer

2D Level Design (programmatically or not) 1 Answer

Orthello MissingReferenceException when loading screens 1 Answer

Overload Vector3's operator + 2 Answers

Cant change transform.position of gameobject 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