How do I allow 3rd Person Character Picking Up Objects within a range?
I am creating a 3rd person puzzle game and I am able to have the character pickup and drop an object that has this script with the 'K' key. The problem is, I can't figure out how to have the object be picked up only when the character gets within a certain range. If I don't do this, any object with this scrip will teleport to the front of the character from any place on the game.
So how do I allow objects that can be picked up only be picked up when the character is close to them?
Here is the script that allows the object to be picked up and dropped using the 'K' key.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Pickable : MonoBehaviour { public GameObject item; public GameObject tempParent; public Transform guide; bool carrying;
// Use this for initialization
void Start()
{
item.GetComponent<Rigidbody>().useGravity = true;
}
// Update is called once per frame
void Update()
{
if (carrying == false)
{
if (Input.GetKeyDown(KeyCode.K))
{
pickup();
carrying = true;
}
}
else if (carrying == true)
{
if (Input.GetKeyDown(KeyCode.K))
{
drop();
carrying = false;
}
}
}
void pickup()
{
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().isKinematic = true;
item.transform.position = guide.transform.position;
item.transform.rotation = guide.transform.rotation;
item.transform.parent = tempParent.transform;
}
void drop()
{
item.GetComponent<Rigidbody>().useGravity = true;
item.GetComponent<Rigidbody>().isKinematic = false;
item.transform.parent = null;
item.transform.position = guide.transform.position;
}
}
Answer by Hellium · Oct 12, 2017 at 04:27 PM
public float range = 5 ;
// ....
if (carrying == false)
{
if (Input.GetKeyDown(KeyCode.K) && (guide.transform.position - transform.position).sqrMagnitude < range * range)
{
pickup();
carrying = true;
}
}
else if (carrying == true)
{
if (Input.GetKeyDown(KeyCode.K))
{
drop();
carrying = false;
}
}
Thank you for the help, I implemented what you put and now it has a range, but it is always fixed at a certain distance no matter how I change the range variable. It looks wrong seeing the object teleport from an unreasonable distance. Any suggestions on how to fix this so that the character has to be right in front of the object to pick it up?
Just changed range * range to range / range and it fixed my problem. Thank you @Hellium for all the help!
Hi! sorry i've been looking everywhere for a script that lets my 3d character pick up an item so i copied this one except i can't figure out how to set it up. The item i wish to pick up is an apple, i gave it a rigidbody, collider and added the script. What should i put in as the guide? and does this script use animations? @Hellium @bdweidner
Your answer
Follow this Question
Related Questions
Passing an object to a function in OnDrop 0 Answers
Hello, Need help with a problem 1 Answer
Pick up, Carry, Throw Object - Mobile Controls 0 Answers
Object Reference Error 1 Answer
Random Object Selection 0 Answers