Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Last active July 10, 2021 00:13
Show Gist options
  • Save jakevsrobots/7416643 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7416643 to your computer and use it in GitHub Desktop.
Script for picking up objects in Unity.
using UnityEngine;
using System.Collections;
public class PickUpObject : MonoBehaviour {
public Transform player;
public float throwForce = 10;
bool hasPlayer = false;
bool beingCarried = false;
void OnTriggerEnter(Collider other)
{
hasPlayer = true;
}
void OnTriggerExit(Collider other)
{
hasPlayer = false;
}
void Update()
{
if(beingCarried)
{
if(Input.GetMouseButtonDown(0))
{
rigidbody.isKinematic = false;
transform.parent = null;
beingCarried = false;
rigidbody.AddForce(player.forward * throwForce);
}
}
else
{
if(Input.GetMouseButtonDown(0) && hasPlayer)
{
rigidbody.isKinematic = true;
transform.parent = player;
beingCarried = true;
}
}
}
}
@CharaKayra69
Copy link

Guys this script for old unity versions so i did make that for new unity version. That Is The Code:
using UnityEngine;
using System.Collections;

public class PickUpObject : MonoBehaviour {
public Transform player;
public float throwForce = 10;
bool hasPlayer = false;
bool beingCarried = false;

void OnTriggerEnter(Collider other)
{
	hasPlayer = true;
}

void OnTriggerExit(Collider other)
{
	hasPlayer = false;
}	

void Update()
{
	if(beingCarried)
	{
		if(Input.GetMouseButtonDown(0))
		{
			GetComponent<Rigidbody>().isKinematic = false;
			transform.parent = null;
			beingCarried = false;
			GetComponent<Rigidbody>().AddForce(player.forward * throwForce);
		}
	}
	else
	{
		if(Input.GetMouseButtonDown(0) && hasPlayer)
		{
			GetComponent<Rigidbody>().isKinematic = true;
			transform.parent = player;
			beingCarried = true;
		}
	}
}

}

@drwho453
Copy link

it does not work

Copy link

ghost commented Jul 10, 2021

set ur collider to is trigger or something idk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment