Raycast on Colliders without Rigidbodies?

Hey all,

I’m currently working on a grid system. Right now I’ve got my grid being generated, and each tile will change colour if it is mouse overed. My question is, how can I achieve this effect without using Rigidbodies on my instantiated tiles? I don’t want my characters, who will be walking on/in these tile objects to kick them about.

Secondary question, the tiles change to a different colour if they are clicked on. How can I change my code so that they stay that different colour once the collision has ended? Thanks!

Code to generate grid:

using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {
	
	public int m_Units;
	private GameObject tempUnitTile;
	private int i;
	private int columns;
	private int rows;
	public Camera m_Camera;
	
	ArrayList GridArray;

private void Awake()
	{		
		GridArray = new ArrayList();
		for (i = 0; i < (m_Units * m_Units); i++)
		{
			tempUnitTile = GameObject.Instantiate(Resources.Load("UnitTile"), new Vector3(columns + 0, 0, rows + 0), Quaternion.identity) as GameObject;
			tempUnitTile.name = "Unit Tile" + (i + 1);
			
			GridArray.Add(tempUnitTile);
			columns++;
			
			if (columns == m_Units)
			{
				columns = 0;
				rows++;
			}
		}
	}
	
	
	private void Update()
	{
		RaycastHit hit;
		string colliderName;
		GameObject m_TileHit;
		
		if (!Physics.Raycast(m_Camera.ScreenPointToRay(Input.mousePosition), out hit, 1000f))
            return;
		
		colliderName = hit.collider.name;
		m_TileHit = hit.collider.gameObject;
        Debug.Log(colliderName);
		
		if (Input.GetMouseButton(0) == true)
		{
			m_TileHit.renderer.material.color = new Color(0f, 0f, 1f, 0.3f);
		}
		else
		{
			m_TileHit.renderer.material.color = new Color(1f, 0f, 0f, 0.3f);
		}

	}
	
}

Code on the tile prefab to reset the colour once collision has ended:

using UnityEngine;
using System.Collections;

public class ColorReset : MonoBehaviour {
	
	private void Update()
	{
		this.gameObject.renderer.material.color = new Color(0.45f, 1f, 0.55f, 0.3f);
	}
	
}

Thanks! :slight_smile:

Colliders and rigidbodies are distinct components, so there is no need to have rigibodies for your tile objects.

The ColorReset sets the tiles to that color every frame, is that what is changing them back?

That’s correct, it’s what turns the tiles back to green when the mouse is no longer hovering over them.

Without the Rigidbody, the collider doesn’t detect the raycast, I’ve always had issues with this, so if I’ve been doing it wrong for over a year, I’d LOVE to know an alternative! :smile:

Edit: Hmm, after removing the Rigidbody it’s still working…It’s a miracle, because it just would NOT work until I added a Rigidbody in the first place!

Thank you for bringing that to my attention, even if it is a little embarassing! :sweat_smile:

Programming games is complicated, there is no reason to be embarrassed by being pretty close but missing something like that.

1 Like

6 years later and this problem still exists along with the magical fix of adding then removing a rigidbody!..

2 Likes

Yeah I totally just found this to be true! Any clue why this is? Is it a bug or a feature or just a trick of the trade?

I have never once had to do this so…no?

So, let me just ask you. Does an object you are doing a Physics.Raycast from need to have a Rigidbody component on it? I ask because when I was coding a behavior on my objects I thought I remember it complaining that it couldn’t do the Raycast because it didn’t have a Rigidbody component on that object. Now many weeks later I removed the Rigidbody component and the Raycast is still working fine. No errors or warnings. Just looking for some clarification on this.
Also, does it matter if the object is set to static or not? Cause initially the object was not static but now it is…so I thought maybe that’s why?

No. Just a collider not set to be a trigger.

No. But also - static objects shouldn’t have Rigidbodies on them because they aren’t meant to move.

1 Like

Thanks for clearing that up for me! Much appreciated!

you can use Trigger Colliders too!