- Home /
Re-spawning issue
alt textAoA Unity community, alt text Im absolutely new to unity and this is my 3rd question. Im working on a tutorial based game. I have done every single bit as per the tutorial guidance. Now that i m trying to re-spawn my player through a fallDetector (an empty body with box collider) , the player just simply falls through it and fall detector never detects it. My code and the snap shots of My Player, FallDetector and checkPoint are all attached. Need your guidance, how can i make fallDetector detect the contact and respawn the player to the defined location.
public class PlayerController : MonoBehaviour {
public float speed = 5f;
public float jumpSpeed = 8f;
float movement = 0f;
Rigidbody2D rigidBody;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask groundLayer;
bool isTouchingGround;
Animator playerAnimator;
public Vector3 respawnPoint;
void Start () {
rigidBody = GetComponent<Rigidbody2D> ();
playerAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
isTouchingGround = Physics2D.OverlapCircle (groundCheckPoint.position, groundCheckRadius, groundLayer);
movement = Input.GetAxis ("Horizontal");
if (movement > 0f) {
rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
transform.localScale = new Vector2 (1f, 1f);
}
else if (movement < 0f) {
rigidBody.velocity = new Vector2 (movement * speed , rigidBody.velocity.y);
transform.localScale = new Vector2 (-1f, 1f);
}
else {
rigidBody.velocity = new Vector2 (0, rigidBody.velocity.y);
}
if (Input.GetButtonDown ("Jump") && isTouchingGround) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x , jumpSpeed );
}
playerAnimator.SetFloat ("Speed", Mathf.Abs (rigidBody.velocity.x));
playerAnimator.SetBool ("onGround", isTouchingGround);
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "fallDetector")
{
transform.position = respawnPoint;
}
if (other.tag == "checkPoint")
{
respawnPoint = other.transform.position;
}
}
}
Thanks in advance to this highly interactive community.
Answer by Sonky108 · Aug 01, 2018 at 12:12 PM
I think that's because you used BoxCollider not BoxCollider2D on your fallDetector. Try changing that and check if it helped.
BoxCollider is used for 3D objects, so I assumed it won't work with any 2D object. Glad it worked! :)
Your answer
Follow this Question
Related Questions
How to refer to run a "void" in one c# script from another to respawn a player 0 Answers
How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers
How to implement a respawn/lives system in Space Shooter (the tutorial game)? 0 Answers
Why can't I make my character die? 3 Answers