void OnCollisionEnter (Collision touch)
{
if(touch.gameObject.name == “flag”)
{
Destroy (touch.gameObject);
}
}
So this my code. when the player collides with the flag it should dissapear, however nothing is happening.
Full code:
using UnityEngine;
using System.Collections;
public class MovementController : MonoBehaviour {
public float maxSpeed = 10f; //new float variable called maxspeed it equals 10
bool facingRight = true; //new bool variable called facingright it equals true
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask WhatIsGround;
public float jumpForce = 700f;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround); //creates circle, groundCheck.position is where the cirle is generated, groundRadius is the radius of the cirle, WhatIsGround is everything it will collide with.
anim.SetBool ("Ground", grounded); // animation bool is Ground = grounded
anim.SetFloat ("vspeed", rigidbody2D.velocity.y);
if (!grounded) return;
float move = Input.GetAxisRaw ("Horizontal"); //how much we are moving
anim.SetFloat ("Speed", Mathf.Abs (move)); //mathf.abs is absoloute value, doesnt matter what direction im moving in.
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); //move * maxSpeed makes the character move depending on what key they're pressing * maxspeed. rigidbody2D.velocity.y keeps our y value the same.
if (move > 0 && !facingRight) //if moving left and not facing left
Flip (); //flip
else if (move < 0 && facingRight) //if moving to the right and not facing right
Flip (); //flip
}
void Update()
{
//DONT DO THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (grounded && Input.GetKeyDown (KeyCode.Space)) // if player is grounded and space is pressed
{
anim.SetBool("Ground", false); // set ground to false
rigidbody2D.AddForce (new Vector2(0, jumpForce));
}
}
void Flip ()
{
facingRight = ! facingRight; // flips the character
Vector3 theScale = transform.localScale; //flips the local scale
theScale.x *= -1; //flip the x axis
transform.localScale = theScale; //apply all of this back to the local scale
}
void OnCollisionEnter (Collision touch)
{
if(touch.gameObject.name == "flag")
{
Destroy (touch.gameObject);
}
}
}