Hi, I’ve only been using Unity for a few weeks, and I’m trying to remake the first level of Sonic the Hedgehog to challenge myself. I’ve run into a roadblock that I can’t figure out.
The red fish that jump up repeatedly at the bridges in the Green Hill Zone are giving me trouble. I need it to be able to ignore the colliders on the bridge from both directions and I need it to be able to recognize Sonic when they collide, so I was thinking about using isTrigger, but when I use isTrigger, it just falls through everything. I was hoping to set another object and use it’s Transform position in an if statement to get the fish to jump each time it gets close to the empty object. This doesn’t seem to be working. Any ideas about how to deal with this problem? Here’s my code for the fish:
public class Chopper : MonoBehaviour
{
//Creates a field in the inspector to link the sonic game object.
public GameObject sonic;
//Creates a variable to hold a SonicController_FSM script component.
private SonicController_FSM sonicScript;
//Creates a field in the inspector to link the associated jump point for chopper.
public Transform jumpPoint;
//Creates a field in the inspector to place chopper's animator.
public Animator chopperAnimator;
//Creates a field in the inspector to place chopper's rigidbody2D.
public Rigidbody2D chopperRb2d;
public CapsuleCollider2D capsuleCollider;
//Set's the force at which the fish will keep jumping in the air.
public float jumpForce = 5;
void Start()
{
sonicScript = sonic.GetComponent<SonicController_FSM>();
chopperAnimator = GetComponent<Animator>();
chopperRb2d = GetComponent<Rigidbody2D>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "SonicPlayer")
{
if (sonicScript.isDeadly)
{
chopperRb2d.isKinematic = true;
chopperAnimator.Play("Enemy_Explosion");
Invoke("Destroyed", 0.3f);
}
}
}
void Update()
{
Jump();
}
private void Jump()
{
//Checks if the choppers's position is less than .2 from the current waypoint's position.
if (Vector2.Distance(jumpPoint.transform.position, transform.position) < 0.2f)
{
chopperRb2d.AddForce(transform.up * jumpForce);
}
}
//Destroys the game object.
private void Destroyed()
{
Destroy(gameObject);
}
}