- Home /
Keys conflicts when shooting or grabbing objects
I have 2 scripts using both the same keys "Fire1" and "E" to perform different actions (shooting and grabbing an item). // SHOOT SCRIPT
void Update()
{
timer += Time.deltaTime;
if (Input.GetButtonDown("Fire1") && timer >= timeBetweenBullets )
{
ShootPlus();
}
}
// GRAB SCRIPT
void Update()
{
playerDistance = Vector3.Distance(item.transform.position, grabber.transform.position);
if (playerDistance >= grabDistance)
{
isGrabbed = false;
}
if (isGrabbed == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(grabber.transform);
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().detectCollisions = true;
if (Input.GetButton("E"))
{
isGrabbed = false;
}
if (Input.GetButton("Fire1"))
{
transform.Rotate(new Vector3 (0,100,0) * Time.deltaTime);
}
}
void OnMouseOver()
{
if (playerDistance <= grabDistance && structureHealth.DOF >= 3)
{
canGrab = true;
}
if (canGrab == true && Input.GetButtonDown("E"))
{
canGrab = false;
isGrabbed = true;
}
}
Desired behaviour:
if the item is not grabbed and player use "Fire1"=> player can shoot
if item is grabbable and "E" is pressed => "isGrabbed = true" and the item is grabbed
if "isGrabbed = true" => the player cannot shoot. Instead, if "Fire1" is pressed, the grabbed item rotates
while the item is grabbed, if "E" is pressed the item is released and the player can shoot again
I guess it's only a matter of logic or using the correct "flags" but I couldn't be able to solve the problem. Anyone can help please?
Answer by blubbr · Apr 19, 2019 at 11:01 PM
I would put multiple if statements checking multiple conditions so the if statement goes down the list checking if the player is grabbing an item, if the player is looking at an item, etc. Sorry but I'm too lazy to type everything out so ill put an outline so you can get the concept im thinking of
//Fire1
if (Input.GetButtonDown("Fire1"))
{
if (isGrabbed == true)
{
//rotate item
} else
{
//shoot
}
}
//E
if (Input.GetButtonDown("E"))
{
if (isGrabbed == true)
{
//release item
isGrabbed = false;
}
if (canGrab == true)
{
//grab item
isGrabbed = true;
}
}
Answer by Bibrosko · Apr 19, 2019 at 11:25 PM
I tried to implement the change for the "E" button but it doesn't work well. I'm not sure where to change the code. as you can see i'm using either "OnMouseOver" and "Update" methods to set the bool "isGrabbed" true or false. I guess those 2 are in conflict because on mouse over, if E is pressed, isGrabbed becomes true, but in the mean time it becomes false in "update". where should I set the bools true/false?
Same thing for the fire function. they belong to 2 different scripts (which i referenced already) but i don't know where i should set the conditions