How to set an Object as a Target with Raycast?

Hi, I have trawled these forums for hours trying to come up with a solution to this and I know that the fix will be easy but i can’t figure it out.

Here’s the deal. I am making a 2.5D RTS for Uni. This code is on my “Engineer” he selects a block and digs away at it like Minecraft.

At the moment the block is a public gameobject “Target”. I need “Target” to be a RayCastHit but I’m having all sorts of problems.

Obviously I don’t know how to use these forums.

Is that in your signature? ( lol )

Condition the Hit - js
if(hit.collider.tag==“Block”)
var curObj = hit.something.something.transform;

The untested code above is just meant to give an idea. :shock:

hehe, you just need to paste the code in as normal text and put [ code ] [ /code ] tags around it… there’s a button that does it for you on the advanced edit/post page.

Yeah I understand that part. What I’m trying to do is point to a block with the mouse and click on it then have him attack the block that I clicked. At the moment he only attacks the block that is in the “Public Gameobject Target”, but he needs to change his block selection when I click o new block.

That’s what i cant work out.

Any help??

screen to world pos check this. The you have to make a raycast from above that position down to your level. Then check if it is a valid block like Black Mantis said.

oh, hit.transform.gameObject will give you the transform/gameObject that was hit. You can also access the rigidbody and colliders via the raycasthit too:

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

use layerMask to make sure you are getting the hit only on the objects you want to test raycast on. and make sure those objects have the proper layer set in the inspector. because if you do not do this, the first object with a collider that was hit will get returned in hit info. by doing it with layer masks your raycast will ignore all the other objects that don’t belong to that specific layer.