Character picks up two cubes simultaneously
So the character picks up a cube by pressing left control, but if I walk too close to another cube and it picks that one up as well. I can't figure out a way to detect if the character is holding a cube or not. Here's the script:
`void OnTriggerStay (Collider collider) {
...
if (collider.transform.tag == "Cube")
{
string cubeName = collider.transform.name.Substring(4, 1);
cubeNum = int.Parse(cubeName);
if (Input.GetKey(KeyCode.LeftControl))
{
if (player.transform.lossyScale.x > 0)
{
cube[cubeNum].transform.position = new Vector3(player.transform.position.x + 1, player.transform.position.y, 0);
}
if (player.transform.lossyScale.x < 0)
{
cube[cubeNum].transform.position = new Vector3(player.transform.position.x - 1, player.transform.position.y, 0);
}
}
}
}`
Here's a little gif to show you what I mean:
Any suggestions?
"I can't figure out a way to detect if the character is holding a cube or not."
Have you considered making that state a property of the player object (EX: IsHoldingCube) that you could set to true when the player first picks up a cube?
Then whenever the player encounters a cube, check that property first. If player.IsHoldingCube == true, do not execute the pickup cube logic. If player.IsHoldingCube == false, execute the pickup cube logic and set player.IsHoldingCube = true.
Your answer
Follow this Question
Related Questions
How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer
Moving instantiated Objects to target locations 1 Answer
How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer
Camera isn't move position? Why my camera isn't change position? 0 Answers