How can my game detect collisions when the player hits the ground

for my game it has to detect 2 things

  1. Is the players colour matching the same as the floor
  2. Is the player actually touching the floor
    Here is the code
using UnityEngine;
using System.Collections;

public class RedBlock : MonoBehaviour {
	public GameObject Player;




	private bool ispr = false;
	private bool ispb = false;
	private bool ispy = false;

	// Use this for initialization
	void Start () {
	

	
	}

	void Update(){
				if (Input.GetKeyDown (KeyCode.Keypad1)) {
						ispr = true; 
						ispb = false;
						ispy = false;
				}


				if (Input.GetKeyDown (KeyCode.Keypad2)) {
						ispr = false;
						ispb = true;
						ispy = false;
				}
				
				if (Input.GetKeyDown (KeyCode.Keypad3)) {
						ispr = false;
						ispb = false;
						ispy = true;
				}
		}
	void debugs(){



			if(ispr == true){
				Debug.Log("Red is true");
				}
			if(ispb == true){ 
				Debug.Log("Blue is true");
			}
			if(ispy == true){
				Debug.Log("Yellow is true");
			}
		}



	// Update is called once per frame
	void OnCollisionStay (Collision col)
	{
		if( col.gameObject.name == "RedGround")
		{
			Destroy(col.gameObject);
		}
	
	
}
	}

thanks

What do you have attached to the Player GameObject?

Is this script being called from the floor or the player itself?

My Player ( Its a cube with some scripts and a rigidbody

As i understand you are using Rigidbody to simulate character movement. Maybe you should prefer CharacterController which has a function or field like IsGrounded so you can check whether it’ s grounded or not. CharacterController, however, doesn’ t have a physical identity from the point of physics engine.

You can do as emre2345DH has stated or you could use a RayCast to recreate this IsGrounded function like this:

using UnityEngine;
using System.Collections;

public class RedBlock : MonoBehaviour {
	public GameObject Player;
	
	
	
	
	private bool ispr = false;
	private bool ispb = false;
	private bool ispy = false;

	void Start () {
		
		
		
	}
	
	void Update(){
		if (Input.GetKeyDown (KeyCode.Keypad1)) {
			ispr = true;
			ispb = false;
			ispy = false;
		}
		
		
		if (Input.GetKeyDown (KeyCode.Keypad2)) {
			ispr = false;
			ispb = true;
			ispy = false;
		}
		
		if (Input.GetKeyDown (KeyCode.Keypad3)) {
			ispr = false;
			ispb = false;
			ispy = true;
		}
	}
	void debugs(){
		
		
		
		if(ispr == true){
			Debug.Log("Red is true");
		}
		if(ispb == true){
			Debug.Log("Blue is true");
		}
		if(ispy == true){
			Debug.Log("Yellow is true");
		}
	}

	void OnCollisionStay (Collision col)
	{
		if( col.gameObject.name == "RedGround")
		{
			Destroy(col.gameObject);
		}
		
		
	}

	bool checkGround() {
		return Physics.Raycast(transform.position, -Vector3.up, PUT IN HALF THE HEIGHT OF THE PLAYER + 0.1);
	}
}

Obviously edit the “PUT IN HALF THE HEIGHT OF THE PLAYER” part, and remember to take into consideration any “Offsets” you may have set.

To use this you would call the checkGround() function. If you add an “out” to the Raycast, you will even be able to detect the gameobject that you hit (enabling you to check its name and colour).

Damn im sorry instead of a rigid body Iv’e got a script that detects collisions but it doesn’t work either
here it is

using UnityEngine;
using System.Collections;


[RequireComponent (typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour {
	
	public LayerMask collisionMask;
	
	private BoxCollider collider;
	private Vector3 s;
	private Vector3 c;
	
	private float skin = .005f;
	
	[HideInInspector]
	public bool grounded;
	[HideInInspector]
	public bool movementStopped;
	
	Ray ray;
	RaycastHit hit;
	
	void Start() {
		collider = GetComponent<BoxCollider>();
		s = collider.size;
		c = collider.center;
	}
	
	public void Move(Vector2 moveAmount) {
		
		float deltaY = moveAmount.y;
		float deltaX = moveAmount.x;
		Vector2 p = transform.position;
		
		// Check collisions above and below
		grounded = false;
		
		for (int i = 0; i<3; i ++) {
			float dir = Mathf.Sign(deltaY);
			float x = (p.x + c.x - s.x/2) + s.x/2 * i; // Left, centre and then rightmost point of collider
			float y = p.y + c.y + s.y/2 * dir; // Bottom of collider
			
			ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
			Debug.DrawRay(ray.origin,ray.direction);
			
			if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) + skin,collisionMask)) {
				// Get Distance between player and ground
				float dst = Vector3.Distance (ray.origin, hit.point);
				
				// Stop player's downwards movement after coming within skin width of a collider
				if (dst > skin) {
					deltaY = dst * dir - skin * dir;
				}
				else {
					deltaY = 0;
				}
				
				grounded = true;
				
				break;
				
			}
		}
		
		
		// Check collisions left and right
		movementStopped = false;
		for (int i = 0; i<3; i ++) {
			float dir = Mathf.Sign(deltaX);
			float x = p.x + c.x + s.x/2 * dir;
			float y = p.y + c.y - s.y/2 + s.y/2 * i;
			
			ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
			Debug.DrawRay(ray.origin,ray.direction);
			
			if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {
				// Get Distance between player and ground
				float dst = Vector3.Distance (ray.origin, hit.point);
				
				// Stop player's downwards movement after coming within skin width of a collider
				if (dst > skin) {
					deltaX = dst * dir - skin * dir;
				}
				else {
					deltaX = 0;
				}
				
				movementStopped = true;
				break;
				
			}
		}
		
		
		
		
		Vector2 finalTransform = new Vector2(deltaX,deltaY);
		
		transform.Translate(finalTransform);
	}
	
}

What you have included there alone won’t do anything… Are you calling the public void Move() from another script on the character or?

no everything is on that script. the only other scripts on the player is one to change the colour of the material

Try to add this to the code (I am not completely sure how this will function as I am not 100% clear about what does and doesn’t work):

Add this part above void Start() {

float movementSpeed = 5;

And this below the Start.

	void Update () {
		float YSpeed = Input.GetAxis ("Vertical") * movementSpeed;
		float XSpeed = Input.GetAxis ("Horizontal") * movementSpeed;

		Vector2 moveSpeed = new Vector2 (sideSpeed, forwardSpeed);

		Move (moveSpeed * Time.deltaTime);
	}

This should then make the UP LEFT DOWN RIGHT or W A S D move the character (and maybe the floor detection work also?) You will have to mess around with the movementSpeed and whether you want them to be forced to -1 and 1 instead of acceleration etc.

It hasn’t changed anything

Damn i forgot there is a script that releys on the script above

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : MonoBehaviour {
	
	// Player Handling
	public float gravity = 20;
	public float speed = 8;
	public float acceleration = 30;
	public float jumpHeight = 12;
	
	private float currentSpeed;
	private float targetSpeed;
	private Vector2 amountToMove;
	
	private PlayerPhysics playerPhysics;
	
	
	void Start () {
		playerPhysics = GetComponent<PlayerPhysics>();
	}
	
	void Update () {
		// Reset acceleration upon collision
		if (playerPhysics.movementStopped) {
			targetSpeed = 0;
			currentSpeed = 0;
		}
		
		// If player is touching the ground
		if (playerPhysics.grounded) {
			amountToMove.y = 0;
			
			// Jump
			if (Input.GetButtonDown("Jump")) {
				amountToMove.y = jumpHeight;	
			}
		}
		
		// Input
		targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
		currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
		
		// Set amount to move
		amountToMove.x = currentSpeed;
		amountToMove.y -= gravity * Time.deltaTime;
		playerPhysics.Move(amountToMove * Time.deltaTime);
	}
	
	// Increase n towards target by speed
	private float IncrementTowards(float n, float target, float a) {
		if (n == target) {
			return n;	
		}
		else {
			float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
			n += a * Time.deltaTime * dir;
			return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
		}
	}
}

Dont worry i figured it out