Greetings all, first post here but I’m sure it won’t be my last!
I’m attempting to grab an object from the world (in this case, a rigidbody table object) and place it slightly in front of the main camera for the duration of a button press, so that it moves and rotates with the camera and retains the metre-or-so distance from the camera, but the angle needs to rotate so that it is always appears to be facing the same direction to the viewer.
If the player grabs the object, it should appear to float and move along with them until they release it, turning as they turn the mouse (I have mouselook working fine) so that they can choose a new place and angle to put their object at.
If anyone can help me out, that’d be great, I’m new to Unity after having a fair amount of VB and Javascript experience, but I’m learning fast!
var playerLocation: Transform;
var xDistance : float = 3;
var yDistance: float = 1;
function Update ()
{
var ray =Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2)); //setting the raycast to select the object in the camera view
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 20))
{
colliderObject = hit.collider.tag;
if (colliderObject == "WorldObject") //check to see if it is a moveable object, tagged 'WorldObject'
{
print(colliderObject); //just so I can see what's going on
}
else
{
print("No movable object"); //is not a moveable object
}
Debug.DrawLine (ray.origin, hit.point);// letting me see what's going on
if(Input.GetButton("Grab")) // grab set to g
{
if (colliderObject == "WorldObject")
{
hit.transform.position = playerLocation.transform.position + playerLocation.transform.forward * xDistance + playerLocation.transform.up * yDistance; // positions the object 1 metre up, 3 metres out
}
}
}
}
Okay this is what I have so far, it works but if you move the mouse too quickly it drops the item, also I can’t move it up and down, or rotate it. Anyone got any suggestions?
The concept of parenting will be covered in just about any introductory tutorial on Unity, I think (I’d guess it’s covered in the tutorials provided on the Unity site, among others).
There’s actually very little to it though. The Transform class has a field/property called ‘parent’. To make an object a child of another, you assign to the ‘parent’ property/field, e.g.:
That is great advice, but I have a slight problem implimenting it. I’m not sure how to un-parent the object afterwards correctly.
The code above uses the object hit by the raycast, so to un-parent an object I have to be looking at it, and I also need the object to turn with the camera, so if the object gets caught on scenery and gets pushed behind the player’s view, they cannot drop the object.
Any suggestions? I realise how much of a noob I sound…!
To keep track of the object, you wouldn’t raycast continuously (which sounds like what you’re suggesting), but would rather store a reference to the object when it’s picked up (or otherwise keep track of it) so that you can drop it when the time comes.
As for getting caught on stuff, I’d think you’d probably want to disable collision for the object while it’s being moved, or perhaps drop it automatically if it runs into something.
No, not really. You could try searching the forums and/or Unity Answers for e.g. ‘pick up item’ or ‘carry item’ and see what you could find there. But, IMO, trying to adapt existing code isn’t necessarily the best way to go about solving problems like this. What I’d recommend instead is to keep working on understanding the concepts yourself, try to code it, and then post to the forums or UA when you have specific questions or need further help.