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
2
Question by davidc · May 03, 2013 at 02:05 PM · 2djavascriptenemysidescroller

Finding Positions of other game objects and using them as an argument for an if statement.

Hey guys, First of all ill just say thank you to everyone who has helped me on this forum, your all a bunch of legends.

Ok so im new to programming and i have attempted to write a code that finds the y position of an enemy and then uses that y position as an argument for a "if statement". Im creating a sidescroll platformer game like mario and what im trying to accomplish with this code is when my character jumps on an enemies head, they die, but if the player touches the enemy from side, then the player looses health. How i think this could be done is by finding the y position of the enemy and then creating an if statement that says (if player is higher then enemy) blah blah blah.

The code i have got so far is this.........

 target = GameObject.FindWithTag("enemy").transform.y;
 myPosition = transform.position.y;
 
 if(myPosition > target){
 Debug.Log("player is up higher then enemy");
 }`

can anyone help? i also just noticed that if i use (findwithTag) then the code might find all the enemies in the level and activate the code when its not supposed too, is that right?. an example would be if enemyA is sitting on a hill and i run into the side of enemyB then the code would activate because they are both tagged with "enemy"

Help, please. any advice will be greatly appreciated.

Dave.

Comment
Add comment · Show 3
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 ByteSheep · May 03, 2013 at 02:16 PM 1
Share

Is your script working? Not sure exactly what you are asking..
Do you want help writing the script or is there a specific problem you are encountering?
One thing I would add is that it would probably be easier to add the script to the enemies ins$$anonymous$$d of the main character. Then you only have to reference the one character, rather than each enemy.

avatar image robertbu · May 03, 2013 at 02:29 PM 1
Share

Note positions are the location of the center of the object (or wherever the author of the mesh set as center), so your code as it stands will detect when the middle of your character is above the middle of the target.

avatar image ATMEthan · May 03, 2013 at 02:42 PM 1
Share

robertbu adds a good note. So to clarify if you want to check if the player is on top of the enemy you'll have to do something like this: (myPosition - half$$anonymous$$yHeight[if myPosition is located in the center]) < (target + half$$anonymous$$yHeight[if enemy position is lcoated in the center]). So, essentially you need to check if (myFeet < enemyHead). But remember you still have to account for the x bounds to ensure your are hitting the right enemy.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ATMEthan · May 03, 2013 at 02:39 PM

Hey Davidc, I can tell you are on the right track. merry_christmas has a good idea. If you put the collision logic on a script that the enemy has you only have to worry about the players position. The player position can be a static variable that collision logic script will hold. This ensures the player position variable is the same for all instances of the collision logic script. One problem you have is that you don't take into account the player/enemy's x position. This will ensure that only the right enemy will be hit. So you have to check if the player is above the enemy(which will be this.transform.position.y - if the script is attached to the enemy) and you have to check that the player is within the bounds of the x value. To do this you'll need to know where the anchor/transform of the enemy is. If its in the center the x bounds will be:

             (this.transform.position.x - enemyWidth / 2) //this is the left x bounds
             (this.transform.position.x + enemyWidth / 2) //this is the right x bounds

The enemyWidth variable is the lenght of the sprite/texture of the enemy on the x axis. Becuase you are in the cetner half the width of the enemy's texture/sprite gets you to the far left or right of the enemy. If the transform/anchor is on the far left of the enemy the code would be:

         (this.transform.position.x) //this is the left x bounds
         (this.transform.position.x + enemyWidth) //this is the right x bounds

So by check the x value of the player against the x bounds of the enemy you will know if the player is actually on top of the enemy or just jump somewhere else. I hope this help, from what I can tell your a smart man on the right path just think and you'll get through this. :D

Comment
Add comment · Show 2 · 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 davidc · May 04, 2013 at 01:29 PM 0
Share

Hey guys, thankyou for all your help, i have worked on the problem all day with your support and suggestions and i have reworked some code and i have come up with this bit of code. it seems to be working fine and im quite proud of myself, especially being a coding noob.

 function OnTriggerEnter(collision : Collider){
     
 
         //replaced all the variables for this line
     if(GameObject.Find("Player").transform.position.y > transform.position.y){
     //applied some simple code, not all necessary to get it to work properly
         GetComponent("BoxCollider").enabled = false;
     GetComponent("$$anonymous$$atoRotate").enabled = false;
     GetComponent("$$anonymous$$ove Left").enabled = false;
     transform.position += Vector3(0, -1, 0);
     transform.localScale += Vector3(0,-3,0);
     yield WaitForSeconds(2.0);
     Destroy (gameObject);
 }
     //then an if statement denoting if the player collides with the enemy without being higher on the y axis
     if(collision.gameObject.tag == ("Player")){
     InvokeRepeating("hurtbyenemy", 0.1, 3);
     }
 }
         //sends message to the main script to apply damage and adjusting GUI
 function hurtbyenemy(){
 gameObject.Find("Player").Send$$anonymous$$essage("Damage");
 CancelInvoke();
 }
 
 

In my endless googleing and forum searching i have noticed a trend of people not finaliasing there solutions, i Thought i would post this up incase anyone encounters the same problems.

Dave..

avatar image S0LiD · Jul 05, 2014 at 11:42 AM 0
Share

Thanks Dave for sharing your solution!

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

15 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to make basic AI in a 2d game? 4 Answers

Uni2D Play() - Misunderstanding 3 Answers

Apply Damage To Player On Collison With Specific Game Object 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