- Home /
Box Pick up and movement
Hey, I have figured out picking up the box and moving it around however I still need a good way to detect if I am looking at the box. right now I have a trigger added to my camera and I am using
Function OnTriggerStay()
however it is not always detecting when the trigger is in a box
here is the script that I am using to interact with the boxes
function Update(){
if(Input.GetKeyDown(KeyCode.F)){
boxaction = true;
}else if(Input.GetKeyUp(KeyCode.F)){
boxaction = false;
}
}
function OnTriggerStay(other : Collider){
Debug.Log("collider is in box");
Debug.Log(other.tag);
Debug.Log(this.tag);
var clone : GameObject;
cube = other.collider.gameObject;
if (other.collider.tag == "Cube" && boxaction && cubehold == false){
cubehold = true;
clone = Instantiate(cube, cube.transform.position, cube.transform.rotation);
Destroy(cube);
clone.transform.parent = Camera.main.transform;
clone.rigidbody.isKinematic = true;
}else if(other.collider.tag == "Cube" && boxaction == false){
cubehold = false;
other.transform.parent = null;
other.rigidbody.isKinematic = false;
}
}
Why is the trigger attached to the camera not always detecting the box?
Also I am having problems that when I pick my boxes up the mesh tends to deform, does anyone know why this is and how I can stop it?
Any help would be appreciated. Thanks
It is quite unclear what is happening and how. Please at least describe the scene in a few words. One can't tell 'what' picks up. The mouse? The player? Where is the script attached?
with the mouse :
private var imSelected : Transform;
private var imClickedOn : boolean = false;
// in function
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var rayHit : RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit))
{
imSelected = rayHit.transform;
if (imSelected == this.transform) {imClickedOn = true;}
}
}
You should swap around your Get$$anonymous$$ouseButtonDown and Raycast statements. Probably better performance if the ray is only initiated with a mouse down event.
Hey, Thanks for the answers. What I am looking for is something like in portal or half life, were you look at a object and hit "e" and then you can move it around and place it where ever you want.
example(http://www.youtube.com/watch?v=UjAp$$anonymous$$HyEGTc)
The scene is: you are standing in a room with 3 cubes and a raised floor. To get on the raised floor you need to stack the cubes so you can jump up them and on to the raised floor. does that help? I guess I should have been a little more specific. also I have the script attached to the main camera.
Answer by Apex5 · Apr 10, 2012 at 08:36 PM
Be aware that OnTriggerStay only registers colliders that have entered it (and have a rigidbody attached); for example if the box starts in the trigger OnTriggerStay will not be called, I would imagine similar things would happen if you teleported the box in without crossing the threshold so to speak.
The way you're going about this seems a bit weird, e.g.
clone = Instantiate(cube, cube.transform.position, cube.transform.rotation);
Destroy(cube);
is meaningless as you're creating a copy of the cube at the exact same position/rotation and then destroying the original, effectively doing nothing; parenting the cube is fine but since you don't move it anywhere ( e.g. to the position of the player's "hands") it might stay out of your trigger's range anyway since it's not getting any close to the player.
In terms of telling what you're looking at you can use a raycast with the Camera's position+forward and then get the gameObject from that. I suggest looking up a full tutorial for this rather than trying to mash together pieces of different people's code (imo).
Yeah the way I was doing was rather weird, I rewrote it using raycasting and it is detecting the boxes much more reliably, anyway thanks for answering my question.