Pick up code issue
Hi Everyone! I need a little help figuring out this code. For some reason with a collision my object doesn't dissapear/hide.
The code is below:
using System.Collections; using UnityEngine.UI; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed; //Floating point variable to store the player's movement speed.
public float jump;
float moveVelocity;
public Text countText;
public Text winText;
private int count;
private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
//Grounded Vars bool grounded = true;
// Use this for initialization
void Start()
{
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D> ();
count = 0;
SetCountText ();
winText.text ="";
}
void Update () { //Jumping if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) { if(grounded) { GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jump); } }
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
}
// // Check if Grounded
void OnTriggerEnter2D()
{
grounded = true;
}
void OnTriggerExit2D()
{
grounded = false;
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
//Store the current horizontal input in the float moveHorizontal.
float moveHorizontal = Input.GetAxis ("Horizontal");
//Store the current vertical input in the float moveVertical.
float moveVertical = Input.GetAxis ("Vertical");
//Use the two store floats to create a new Vector2 variable movement.
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
rb2d.AddForce (movement * speed);
// Update is called once per frame. this is where our physics code will go
}
//OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
void OnTriggerEnter2D(Collider2D other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
void SetCountText ()
{
countText.text ="countText:" + count.ToString();
if (count>=12){
winText.text= "You Win!";
}
}
}
Answer by PizzaPie · Dec 24, 2016 at 07:56 PM
The problem is that you have 2 OnTriggerEnter2D functions it never calls the second. Instead use only one like this: void OnTriggerEnter2D(Collider2D other) { grounded = true; if (other.gameObject.CompareTag("PickUp")) { other.gameObject.SetActive(false); } } Also when you disable the object it will never call the OnTriggerExit(). Hope that helps.
Your answer
Follow this Question
Related Questions
Collision not working 1 Answer
Collisions happening though disabled in matrix 2 Answers
How do I make my player jump? 1 Answer
Prevent "useless" character movements 0 Answers
Collisions not working in top-down RPG? 0 Answers