My main character isn't being able to colide with the objects.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float movespeed;
public float jumpheight;
public Transform Groundcheck;
public float GroundcheckRadius;
public LayerMask WhatIsGround;
private bool grounded;
private bool doubleJump;
private Animator anim;
// Use this for initialization
void Start() {
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(Groundcheck.position, GroundcheckRadius, WhatIsGround);
}
// Update is called once per frame
void Update() {
if (grounded)
doubleJump = false;
if (Input.GetKeyDown(KeyCode.W) && grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpheight);
salto();
}
if (Input.GetKeyDown(KeyCode.W) && !doubleJump && !grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpheight);
salto();
doubleJump = true;
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(movespeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-movespeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat("Speed",Mathf.Abs (GetComponent < Rigidbody2D>().velocity.x));
}
public void salto()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpheight);
}
}
So this is main character code. I will show aswell the Script that i use to when i collide with spikes he dies.
using UnityEngine;
using System.Collections;
public class KillPlayer : MonoBehaviour
{
public LevelManager levelManager;
// Use this for initialization
void Start()
{
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Kappa kek")
{
levelManager.RespawnPlayer();
}
}
}
This was all working until i tried to place the respawn particles and death particles working out. Since i placed this code the Colision isn't working, the Player passes right in the middle of the object nothing happens. And before i placed this code with the particles everything was fine...
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour
{
public GameObject currentCheckpoint;
private PlayerController player;
public GameObject deathParticle;
public GameObject respawnParticle;
public float respawnDelay;
// Use this for initialization
void Start()
{
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update(){
}
public IEnumerator RespawnPlayer()
{
Instantiate(deathParticle, player.transform.position, player.transform.rotation);
player.enabled = false;
player.GetComponent<Renderer>().enabled = false;
Debug.Log("Player Respawn");
yield return new WaitForSeconds(respawnDelay);
player.transform.position = currentCheckpoint.transform.position;
player.enabled = true;
player.GetComponent<Renderer>().enabled = true;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
public void RespawnPlayerCo()
{
StartCoroutine("RespawnPlayerCo");
}
}
Comment
i was able to fix the error deleting IEnumerator and placing a void.
i also deleted yield return new WaitForSeconds(respawnDelay)
and i added a Debug.Log in the $$anonymous$$illplayer Class
Just in case someone has the same problem. Don't use IEnumerator if you are not in loop.