interaction/destroy with object in the scene

Hi!

i’m trying to make an interactive camera like the one in oblivion or in sherlock holmes the awakened for example, where you can examine and pick objects in the scene.

I add a cube to the camera (inside the main camera in the first person controller) in order to check collisions with other objects and have exact control of the aiming size.

A screenshot of who the camera looks like: ImageShack - Best place for all of your image hosting and image sharing needs

all the code is in the OnTriggerStay event, so when the player is pointing to the object and press the “E” key, the object is destroyed.
The problem is that it returns the first person camera controller, not the object that is touching.

Anyone knows how to fix this problem.

i add the code snipet:

function OnTriggerStay(other : Collider) {

objectInteract = other.gameObject;

if (Input.GetKeyDown (KeyCode.E)) {

Debug.Log(objectInteract.name); HERE IT RETURNS THE FIRST PERSON CONTROLLER

///Destroy(objectInteract);
}

Thanks in advance!!!

I can’t tell for sure but since your script in on the object you want to interact with, the OnTriggerStay event is telling you about other rigidbodies that it is colliding with. In this case, that is the first person camera controller which you don’t want to delete.

I think you just want to delete the object itself.

function OnTriggerStay(other : Collider) {

   if (Input.GetKeyDown (KeyCode.E)) {
      Destroy(gameObject);
   }
}

Thanks for the reply Kenlem, but now is the cube that acts like hand what returns, i would like to destroy the other object, the one that the “hand” is touching.

i think i did not explain it correct, but the script is in the cube that acts like the hand, the one that is highlighted in the screenshot.

Hi Esteban,

I tried something like this the other day (giving the camera a box collider), but got the same result as you. I hope someone can explain how to achieve this.

Perhaps I need to check all the objects that are intersecting the collider, remove the camera/player and then pick the closest to the camera (in case the collider is touching multiple objects).

Does this topic help Esteban? It doesn’t use the box like your example, but you should be able to achieve the same thing.

http://forum.unity3d.com/viewtopic.php?t=6786&highlight=crosshair

Thanks for the link whtrbt, i will try this way.

I’m working on the same thing right now, got the raycast working correctly last night (just changing a crosshair texture to show when the ray hits an object), and now I’m going to try to identify which object it is and then call a function on a script attached to the object.

I’ll let you know how I go. :slight_smile:

Okay, success! I put this script on my first person camera (I’m using a GUITexture at the centre of the screen for the texture, you can remove the code relating to this to get the script working).

var crosshair : GUITexture;
var offTexture : Texture2D;
var onTexture : Texture2D;


function Start() 
{
	Screen.lockCursor = true;
} 
 

function Update ()
{
	
	var layerMask = 1 << 8;
	var hit : RaycastHit;

	if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask))
		{
			crosshair.texture = onTexture;
			
			if (Input.GetKeyDown (KeyCode.E))
			{
				var hitScript : InteractionScript = hit.transform.GetComponent (InteractionScript);
				hitScript.Activate ();
			}
			
		} else {
			crosshair.texture = offTexture;
		}
	}

And I put this script on the mesh of objects I want to activate when I press E. I called it InteractionScript, you’ll have to update the above code to change the name of that.

function Activate ()
{
	print ("Activated " + name);
}

And finally, you have to put the objects with the script on it onto Layer 8 (in the top right of the Inspector).

I think I’m going to try to update it so it only works within a certain distance of the player, so you can’t grab/use objects from miles away.

The screenshots you posted at the start of the thread look interesting - can you post a good shot of what you’re working on?

This is really helpfull whtrbt, thanks for sharing!!!

The screenshot is from an example project:

http://unity3d.com/support/resources/example-projects/sewercontrolroom.html

No problem!

I’ve changed the script slightly since, to support different behaviours for different objects… I can keep you updated as I go, if you’d like.

That would be great, thanks!!!