Hey guys.
I’m making a 2D Platformer game.
Right now I’m working with the enemy script. On my enemy there is a collider2D parented with the enemy.
When the player get in contact with the parented collider the stomp bool goes to true. The problem with that is, if the player stays idle and the enemy get contact with the player then the enemy dies and the current health from the player falls down.
So now I want to make it so when the player is not grounded(that means he is jumping right now) and get contacted with the collider, then the stomp goes to true not otherwise. In my PlayerControllerScript is a variable “bool grounded = false;” with a groundcheck.
How can I implement this into my stomp.cs script?
Image from the enemy:
Here is the stomp script:
using UnityEngine;
using System.Collections;
public class stomp : MonoBehaviour {
public string tag = "Player";
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == tag)
{
transform.root.gameObject.GetComponent<SendDamageCollider>().stomp = true;
}
}
}
My second problem…
When the stomp gets true in the SendDamageCollider script everything works great except “if(fall)”. The problem there is, that the x axis from the enemy goes to 0 when the enemy is falling. I want it that the x axis stays same as the enemy. In the “if(stomp)” the x axis is the same as the enemy. I don’t know why. The SendDamageCollider script is residing with the enemy.
Code snippet from my SendDamageCollider:
public bool stomp;
public bool fall;
void Update ()
{
if(stomp)
{
gameObject.GetComponent<Walker>().walkSpeed = 0.0f;
transform.position = new Vector3 (transform.position.x, transform.position.y, 4);
transform.localScale = new Vector3(2, 1, 2);
fall = true;
//stomp = false;
}
if(fall)
{
transform.position -= new Vector3 (transform.position.x, 0.05f);
}
if (transform.position.y < -16)
{
Destroy (gameObject);
}
}
I hope you guys can help me and also I hope that i could explain it. I’m a newbie at scripting. Sorry for that.
Edit:
Here is a video, what i exactly mean.
https://www.youtube.com/watch?v=TxCE0hrIRwo