- Home /
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.
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.
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.
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.
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
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..