Question by
SecretlyBatman · Feb 04, 2017 at 02:45 AM ·
c#3dpickup
Pickup objects pass through other objects
basically, I modified a C# script I found so that the player can pickup objects, however, the objects that are picked up can pass through other objects in the game. it works, but I have been struggling to make this script do two things; 1) pick up only objects with the tag "pickup" 2) make the object that is picked up not go through other objects (the second one is more important) this is my script:
GameObject grabbedObject;
GameObject lookingObject;
GameObject GetMouseHoverObject(float range)
{
Vector3 position = gameObject.transform.position;
RaycastHit raycastHit;
Vector3 target = position + Camera.main.transform.forward * range;
if (Physics.Linecast (position, target, out raycastHit))
{
return raycastHit.collider.gameObject;
}
return null;
}
void TryGrabObject(GameObject grabObject)
{
if (grabObject == null || !CanGrab(grabObject))
{
return;
}
grabbedObject = grabObject;
}
bool CanGrab(GameObject candidate)
{
return candidate.GetComponent<Rigidbody>() != null;
}
void DropObject()
{
if (grabbedObject == null)
{
return;
}
if (grabbedObject.GetComponent<Rigidbody>() != null)
{
grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
}
grabbedObject = null;
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (1))
{
if (grabbedObject == null)
{
TryGrabObject (GetMouseHoverObject (5));
}
else
{
DropObject();
}
}
if (grabbedObject != null)
{
Vector3 newPosition = gameObject.transform.position + Camera.main.transform.forward * 3;
grabbedObject.transform.position = newPosition;
}
}
Comment
Your answer

Follow this Question
Related Questions
How Do I make a 3D Numbered Padlock? 0 Answers
Airstrike in a area around the player 1 Answer
I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer
Help with consuming an object on key press 1 Answer
How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer