- Home /
Moving a box.
I'm trying to make it so that when the player approaches the box and holds the "e" button he is able to move the box. This is what I have so far: using UnityEngine;
class MoveBox : MonoBehaviour { public Transform player; public bool hold = false;
void Start()
{
this.transform.parent = null;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
hold = true;
}
if (Input.GetKeyUp(KeyCode.E))
{
hold = false;
}
if (hold == false)
{
this.transform.parent = null;
Debug.Log("released");
}
}
void OnCollisionStay(Collision col)
{
if (col.gameObject.tag == "Player" && hold == true)
{
this.transform.parent = player;
Debug.Log("holding");
}
}
}
But it isn't working, it notices the player holding the "e" button from any distance instead of just when he is near the box. Also the box will only move if he is holding it.
btw. when checking for something to be true, you dont need to say: hold == true ... you can just say: if (hold) ... something...
thats why i usually name ALL my bool's with is at the beginning and in your case I would use "bool isHolding" because then I can check it with if (isHolding) { // do something} and if (!isHolding)
Answer by Ludiares.du · Oct 29, 2011 at 03:38 PM
Add a Vector3.Distance Statement. Something like:
if(Vector3.Distance(transform, GameObject.Find(Player).transform))
{
//Do stuff
}
But how would I get the player to move the box when he is within that distance?
Your answer
Follow this Question
Related Questions
How to hold objects in third person? 1 Answer
My weapon takes damage. help... 1 Answer
Trigger help! 0 Answers
Ammo Pick up C# Not Working? 0 Answers
Player should move box, Ai shouldn't 1 Answer