estou começando no unity agr e estou com muita duvida em como resolver isso
5141228–508967–Player.cs (2.45 KB)
estou começando no unity agr e estou com muita duvida em como resolver isso
5141228–508967–Player.cs (2.45 KB)
I think, nested function don’t have access modifier and you can’t put OnCollisionEnter/Exit inside the FixedUpdate
use code tags for the scripts:
(declaration is missed for TextLives.text. Try to handle Input in the Update it is better as in the FixedUpdate. Do reference (Rigidbody2D rigidbody = GetComponent() ; once in the Start (don’t forget to declare it).)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float forcapulo;
public bool estaVoando;
public string nome;
// comparação if
public float tempo;
public float distancia;
public float velocidade;
public float velocidadeMaxima;
public float velocidadeMinima;
public int Lives;
public int Coins;
public Text TextCoins;
public bool IsGrounded;
public bool CanJumpAtack;
void Start()
{
TextLives.text = Lives.ToString();
TextCoins.text = Coins.ToString();
}
void FixedUpdate()
{
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
float movimento = Input.GetAxis("Horizontal");
rigidbody.velocity = new Vector2(movimento * velocidadeMaxima, rigidbody.velocity.y);
if (movimento < 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if (movimento > 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
if (movimento > 0 || movimento < 0)
{
GetComponent<Animator>().SetBool("Walking", true);
}
else
{
GetComponent<Animator>().SetBool("Walking", false);
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded)
{
rigidbody.AddForce(new Vector2(0, forcapulo));
GetComponent<AudioSource>().Play();
CanJumpAtack = false;
}
else
{
CanJumpAtack = true;
}
}
if (CanJumpAtack && Input.GetKey(KeyCode.Space))
{
GetComponent<Animator>().SetBool("JumpAtack", true);
rigidbody.velocity = new Vector2(rigidbody.velocity.x, -2f);
}
else
{
GetComponent<Animator>().SetBool("JumpAtack", false);
}
if (IsGrounded)
{
GetComponent<Animator>().SetBool("jumping", false);
}
else
{
GetComponent<Animator>().SetBool("jumping", true);
}
}
private void OnTriggerEnter2D(Collider2D collision2D)
{
if (collision2D.gameObject.CompareTag("Moedas"))
{
Destroy(collision2D.gameObject);
Coins++;
TextCoins.text = Coins.ToString();
}
}
void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.gameObject.CompareTag("Monstros"))
{
//Criar Lógica para perder vida
}
if (collision2D.gameObject.CompareTag("Plataformas"))
{
IsGrounded = true;
}
{
Debug.Log("Colidiu!" + collision2D.gameObject.tag);
}
}
void OnCollisionExit2D(Collision2D collision2D)
{
if (collision2D.gameObject.CompareTag("Plataformas"))
{
IsGrounded = false;
}
Debug.Log("Parou de colidir!" + collision2D.gameObject.tag);
}
}
O que vc fez para consertar esse erro? eu n entendi direito
here is the code wrong
void Update ()
{
//__________________________________
//This is wrong because the function is inside from the update.
void OnTriggerEnter2D()
{
}
//___________________________________
}
here is the code ok
void Update ()
{
//_______________
//_______________
}
//This will work because the function is outside from the update.
void OnTriggerEnter2D()
{
}
vc pode me ajudar nesse codigo aki? está dando o mesmo erro
5149826–510089–Player.cs (3.74 KB)
Be careful with “{ }”. No idea, how they are named
Check the lines 106 and 109
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float forcapulo;
public bool estaVoando;
public string nome;
// comparação if
public float tempo;
public float distancia;
public float velocidade;
public float velocidadeMaxima;
public float velocidadeMinima;
public int Lives;
public int Coins;
public Text TextLives;
public Text TextCoins;
public bool IsGrounded;
public bool CanJumpAtack;
public GameObject LastCheckpoint;
void Start()
{
TextCoins.text = Coins.ToString();
TextLives.text = Lives.ToString();
}
void FixedUpdate()
{
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
float movimento = Input.GetAxis("Horizontal");
rigidbody.velocity = new Vector2(movimento * velocidadeMaxima, rigidbody.velocity.y);
if (movimento < 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if (movimento > 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
if (movimento > 0 || movimento < 0)
{
GetComponent<Animator>().SetBool("Walking", true);
}
else
{
GetComponent<Animator>().SetBool("Walking", false);
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded)
{
rigidbody.AddForce(new Vector2(0, forcapulo));
GetComponent<AudioSource>().Play();
CanJumpAtack = false;
}
else
{
CanJumpAtack = true;
}
}
if (CanJumpAtack && Input.GetKey(KeyCode.Space))
{
GetComponent<Animator>().SetBool("JumpAtack", true);
rigidbody.velocity = new Vector2(rigidbody.velocity.x, -2f);
}
else
{
GetComponent<Animator>().SetBool("JumpAtack", false);
}
if (IsGrounded)
{
GetComponent<Animator>().SetBool("jumping", false);
}
else
{
GetComponent<Animator>().SetBool("jumping", true);
}
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
GetComponent<Animator>().SetTrigger("Atack");
Collider2D[] colliders = new Collider2D[3];
transform.Find("AtackArea").gameObject.GetComponent<Collider2D>()
.OverlapCollider(new ContactFilter2D(), colliders);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i] != null && colliders[i].gameObject.CompareTag("Monstros"))
{
Destroy(colliders[i].gameObject);
}
}
}
}
} //I have added the "}" here
private void OnTriggerEnter2D(Collider2D collision2D)
{
if (collision2D.gameObject.CompareTag("Checkpoint"))
{
LastCheckpoint = collision2D.gameObject;
}
}
void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.gameObject.CompareTag("Monstros"))
{
Lives--;
TextLives.text = Lives.ToString();
if (Lives == 0)
{
transform.position = LastCheckpoint.transform.position;
}
}
if (collision2D.gameObject.CompareTag("Plataformas"))
{
IsGrounded = true;
}
{
Debug.Log("Colidiu!" + collision2D.gameObject.tag);
}
}
void OnCollisionExit2D(Collision2D collision2D)
{
if (collision2D.gameObject.CompareTag("Plataformas"))
{
IsGrounded = false;
}
Debug.Log("Parou de colidir!" + collision2D.gameObject.tag);
}
}
//I have deleted "}" here