I’m just starting out with c# and unity, and I’ve been making a game where you can pick up and throw moving ai as a big player (e.g. think AOT type, so small ai, big player).
I’ve followed a pretty easy tutorial on it. (Link here: EASILY Pick up and Throw Objects | Unity 2018 Tutorial - YouTube)
However, I’ve tried altering the distance for the pick-up, yet it still won’t pick up. I don’t know if this is because the code is for non-movable objects, or that’s it a 2018 tutorial or because I’ve parented the guide to the player’s arms instead of the camera. If you could help, that would be much obliged.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PickUp : MonoBehaviour { float throwForce = 600; Vector3 objectPos; float distance; public bool canHold = true; public GameObject item; public GameObject tempParent; public bool isHolding = false; // Update is called once per frame void Update() { distance = Vector3.Distance(item.transform.position, tempParent.transform.position); if (distance >= 1f) { isHolding = false; } //Check if isholding if (isHolding == true) { item.GetComponent().velocity = Vector3.zero; item.GetComponent().angularVelocity = Vector3.zero; item.transform.SetParent(tempParent.transform); if (Input.GetMouseButtonDown(1)) { item.GetComponent().AddForce(tempParent.transform.forward * throwForce); isHolding = false; } } else { objectPos = item.transform.position; item.transform.SetParent(null); item.GetComponent().useGravity = true; item.transform.position = objectPos; } } void OnMouseDown() { if (distance <= 1f) { isHolding = true; item.GetComponent().useGravity = false; item.GetComponent().detectCollisions = true; } } void OnMouseUp() { isHolding = false; } }