I was trying to test some code for something and what i basically want to happen is when the player comes in range of the enemy. The enemy will jump over the player till it gets right above the player where it will then slam back down. For now i just wanted to get it so the enemy will stop when it’s above the player but it instead just jumps past the player and i don’t know why.

I am new to this so that’s most likely a big reason why
using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

    PlayerController player;

    public float playerRange;
    public LayerMask playerLayer;
    public bool playerInRange;

    public bool jumped;
    public float jumpHeight;

    // Use this for initialization
    void Start () {

        player = FindObjectOfType<PlayerController>();

    }
	
	// Update is called once per frame
	void Update () {

        playerInRange = Physics2D.OverlapCircle(transform.position, playerRange, playerLayer);

        if (playerInRange && !jumped)
        {

            GetComponent<Rigidbody2D>().velocity = new Vector2(player.transform.position.x, jumpHeight);
            jumped = true;
            StartCoroutine(jumpWait());
        }
            if (transform.position.x == player.transform.position.x) {

                transform.position = new Vector3(player.transform.position.x, transform.position.y, 0);

            }

        }

    

    void OnDrawGizmosSelected()
    {

        Gizmos.DrawSphere(transform.position, playerRange);



    }

    IEnumerator jumpWait()
    {
        yield return new WaitForSeconds(5);
        jumped = false;
    }

}

Line 31…

 if (transform.position.x == player.transform.position.x)

You generally can’t compare floating point values for equality when one of them is changing over time. The odds of the two x values being equal are very slim. Instead you’d want to see if they are within some value…

if (Mathf.Abs(transform.postion.x - player.transform.position.x) < 1)...

And even 1 may be too large depending on how fast they’re moving so you may need to play around with that value a bit.

Perhaps a better way to do this would be to set up a trigger on the player and then use OnTriggerEnter. When the enemy enters the trigger the OnTriggerEnter even will be called and you can do your “slam down” code in there.