- Home /
Picking Up AND Hold Onto Items using Raycasting (Bug)
Ok, going t re-edit this to more specific, my problem in a nutshell is that I have set up a ray cast to pick up an object which I can do. However, after roughly 3 seconds or so the player (fps) drops the box and I can't work out why. Set up is a typical ScreenToPoint raycast, the ray cast itself works perfectly, however if I hold the item in the air, even for perfectly still, the item drops out of the air after a few seconds, thus what could be happening. Code to lift the item, is shown below:
hit.collider.gameObject.transform.position = ray.GetPoint(5.5f);
hit.collider.gameObject.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
How about actually adding information, rather than just editing to try to bump. Your question has not been answered because it is too vague.
It's a brief question, for a brief problem. I have no idea why it's happening, therefore I can only explain the problem, which is really a simple bug. I've re-edited my problem, to make it more specific to the problem at hand.
Answer by JimNix · Aug 08, 2011 at 06:03 PM
Just solved it.
hit.collider.gameObject.transform.position = crosshair.GetPoint(4.0f);
hit.collider.gameObject.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
hit.collider.gameObject.rigidbody.velocity = new Vector3(0.0f, 0.0f, 0.0f);
Gravity was (negative y vector) accumulating or at least fighting to pull the object down, however, setting the velocity to 0 while, the object it being held stops this happening. Once the the boolean is false (i.e. GetButton, or isHoldingObject etc) gravity, being a universal force, will be re-applied to the object, and it will fall.
Answer by Waz · Aug 08, 2011 at 05:37 PM
Yes, you have a bug. It is not in the code shown in the question, therefore it is elsewhere in your code.
Thank you for the reply. It's weird as the ray cast works perfectly well for all other functionality, i.e. opening doors. And the method to pick up items, and move them also works well. But only after roughly 3 seconds of picking up the item (either still or moving) does the item drop. It's like the ray is losing contact or something. However, when I hold the ray over it, to return the Object name, the name is returned constantly without a "lose of contact".
$$anonymous$$y guess is that the object is falling because you only position it in Update, not FixedUpdate, and don't adjust it's velocity.
Your answer
Follow this Question
Related Questions
Problem With Raycast Pickup Using Touch 0 Answers
Should Items spherecast 0 Answers
how do i fix this small error 2 Answers
Math - calculate position in world space from ray on infinite plane 2 Answers