Generating raycasts to hit an object

Hey

So I’ve got a scene which contains a bunch of cubes. I need to write a script that allows me to individually select a cube that I click on and apply a function to the cube.

I know I’d need to use raycasts to be able to click individual cubes but I am unsure on how to write the script.

One thing the script would have to do is only have one cube “selected” at any time. For example if I click on one cube that cube would turn red, if another cube is clicked that one turns red but the original goes back to it’s default state (only one cube can be selected at any time).

Could somebody help out with this? Thanks

the cube’s need a script attached which handles the change of colour and back (going to call them Highlight() and Revert() for this example).

creating a ray from the mouseposition is covered in the example here:

you will need one of the raycast functions which return a “RayCastHit” so you can use the hit to reference the cube that has been clicked on

you will need to store the currently selected gameobject (or cube script) as a variable, check the hit.gameobject (or script if you do it that way) is the same or different and call the correct function from the cubescript

hit.GetComponent<CubeScript>().Highlight();
currentSelectedGameObject.GetComponent<CubeScript>().Revert();

changing colour

I’ve attached that Input.Mouseposition to the camera, but the script does not seem to work. I added a debug.log to:

if (Input.GetButtonDown(“Fire1”)) {

but nothing is printed to the console. The script has a GameObject called ‘particle’ but I am unsure on what to do with this.

I added the Physics.Raycast to the cube and that doesn’t print anything to the console either.

What would I do to fix this issue?

post you’re entire script and we can have a look at it, vague description and a single line isn’t as useful.

use [code ][/code ] tags when pasting code into the forums, really helps with the readability. There is a sticky on them at the top of the scripting forum (they’re not the most obvious in the input interface :frowning: )

Used for the camera

using UnityEngine;
using System.Collections;

public class TargetSetter : MonoBehaviour {
public GameObject particle;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray))
Instantiate(particle, transform.position, transform.rotation);
}
}
}

Used for the cube

using UnityEngine;
using System.Collections;

public class CubeHit : MonoBehaviour {
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10)) {
print("There is something in front of the object!");
}
}
}

ok… first code chunk is going to instantiate a “particle” at the same location as the camera, given that the camera is then going to be inside the mesh, it’s not going to appear…

Now I’ve got

using UnityEngine;
using System.Collections;

public class CubeHit : MonoBehaviour {
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10)) {
print("There is something in front of the object!");
}
if (Input.GetMouseButtonDown(0)) {
print ("hit");
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
if (hit.collider != null)
hit.collider.enabled = false;
}
}
}

but this still doesn’t work. This is attached to the cube object by the way.