Hey, I am a total newbie when it comes to unity and coding and I’m trying to make a frogger type game.
I’m having trouble with my player not detecting vehicles, and I can’t seem to find a solution. It was working before I started to mess with the water, but even after un-doing all that, it still doesn’t work.
Thanks in advance for any help!
void Update()
{
if (playerIsAlive == true)
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
if (x != 0 || y != 0)
{
if (!isWalking)
{
isWalking = true;
anim.SetBool("isWalking", isWalking);
}
Move();
}
else
{
if (isWalking)
{
isWalking = false;
anim.SetBool("isWalking", isWalking);
}
}
}
}
void LateUpdate()
{
if(playerIsAlive == true)
{
if (isInWater == true && isOnPlatform == false)
{
KillPlayer();
}
}
}
private void Move()
{
if (playerIsAlive == true)
{
anim.SetFloat("x", x);
anim.SetFloat("y", y);
transform.Translate(x * Time.deltaTime * moveSpeed, y * Time.deltaTime * moveSpeed, 0);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.transform.GetComponent<Vehicle>() != null)
{
KillPlayer();
print("hit");
}
else if (collision.transform.GetComponent<Platform>() != null)
{
transform.SetParent(collision.transform);
isOnPlatform = true;
}
else if (collision.transform.tag == "Coin")
{
playerCanMove = false;
}
else if (collision.transform.tag == "Water")
{
isInWater = true;
GetComponent<AudioSource>().PlayOneShot(drownNoise);
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (playerIsAlive == true)
{
if (collision.transform.GetComponent<Platform>() != null)
{
transform.SetParent(null);
isOnPlatform = false;
}
else if (collision.transform.tag == "Water")
{
isInWater = false;
}
}
}
void KillPlayer()
{
playerIsAlive = false;
playerCanMove = false;
isWalking = false;
GetComponent<SpriteRenderer>().sprite = deadSprite;
}