I have one problem with my pick up and carry objects script. The problem is, the objects can go through the walls (other objects). Is there any way to fix this problem? Thank you!
[GIF] Flying away problem: FlyingAway - Gifyu
P.S. parent_OnHand it’s just an empty gameObject with no components.
The new script:
var onhand : Transform;
public var Distance : float;
public var pickUp_Distance : float = 5.5f;
var player : Transform;
var MC : Transform; // Main Camera
static public var carrying : boolean;
private public var canCarry : boolean = false;
public var rig : Rigidbody;
public var obj : GameObject;
var ray : float = 4.0f;
function FixedUpdate()
{
if(carrying)
{
if(rig != null)
{
rig.MovePosition(onhand.position);
}
}
}
function Update()
{
if(!carrying)
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast(transform.position, fwd, hit))
{
if(hit.distance <= ray && hit.collider.gameObject.tag == "pickupable" && hit.collider.gameObject.GetComponent.<Rigidbody>() != null && carrying == false)
{
canCarry = true;
Debug.Log("Found!");
rig = hit.collider.gameObject.GetComponent.<Rigidbody>();
obj = hit.collider.gameObject;
}
else
{
canCarry = false;
Debug.Log("Missed..");
rig = null;
obj = null;
}
}
}
if(Input.GetMouseButtonDown(1) && canCarry == true && carrying == false)
{
carrying = true;
if(rig != null && obj != null)
{
rig = hit.collider.gameObject.GetComponent.<Rigidbody>();
obj = hit.collider.gameObject;
rig.useGravity = false;
obj.transform.parent = GameObject.Find("Main Camera").transform;
}
}
if(Input.GetMouseButtonUp(1) && carrying == true)
{
carrying = false;
if(rig != null && obj != null)
{
rig.useGravity = true;
obj.transform.parent = null;
}
}
if(carrying)
{
onhand.position = MC.transform.position + MC.transform.forward * 5;
if(rig != null)
{
rig.velocity = Vector3.zero;
rig.angularVelocity = Vector3.zero;
}
}
}