This question was
closed Jul 28, 2016 at 07:34 PM by
Sami_Pal for the following reason:
I found a solution :)
When the Player gets near a hanging enemy, the enemy falls on the player
Hey everyone
I making a 2d endless runner game, I tried to make an enemy which is the Trash Can , the Trash is suspending in the air above the platforms and the player, now when the player gets near the Trash, it will fall down quickly... it simple I guess!
I did some code, but it didn't work.. the player just runs normally below the Trash and it doesn't fall ?
using System.Collections;
using UnityEngine;
public class Trash : MonoBehaviour
{
private Rigidbody2D trashRigid;
private PlayerController thePlayer;
// I tried to make a Player GameObject so I would drag it in Unity, but when I move the Trash to Prefabs, it's gone from the slot!!? and also didn't work :\
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
trashRigid.gravityScale = 0;
trashRigid.constraints = RigidbodyConstraints2D.FreezePositionY;
// I tried to do each one solo {I mean gravity and Freezing Y } but it didn't work too
}
//void OnTriggerEnter2D(Collider2D other)
void Update()
{
if (Vector3.Distance(transform.position, thePlayer.transform.position) < 10)
{
trashRigid.gravityScale = 2;
trashRigid.constraints = RigidbodyConstraints2D.None;
}
}
}
Comment