if statements working strange
It seems that i somehow meessed with IF statements.
In my idea it suposed to only add impulse when i press q or e and there are some charges, but even if there are none it still applyes impulse As i understand if (1 && 2) ___statement suposed to respond to statement only when 1 and 2 are both true, and ignore it otherwise.
What am i doing wrong? Thanks in advance.
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour
{
public float Speed = 800f; //Speed of movement
public float Imp = 200f; //Impulse force
public float MaxSpeed;
public int charges = 5;
private float v; //vertical and
private float h; //horizontal axis;
//private float y = 0f; //zero on y;
private Vector3 MoveVector;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void Update ()
{
v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");
MoveVector = Vector3.zero;
MoveVector = new Vector3(h, v, 0); // we need to keep player on a plane
MoveVector = MoveVector.normalized;
MoveVector *= Speed;
rb.AddForce(MoveVector, ForceMode.Force);
//if(Input.GetKey(KeyCode.LeftShift))
//{
if (Input.GetKey(KeyCode.Q) && charges > 0)
rb.AddForce(Vector3.left * Imp, ForceMode.Impulse);
if (Input.GetKey(KeyCode.E) && charges > 0)
rb.AddForce(Vector3.right * Imp, ForceMode.Impulse);
//if (Input.GetKey(KeyCode.W) && charges > 0)
// rb.AddForce(Vector3.up * Imp, ForceMode.Impulse);
//if (Input.GetKey(KeyCode.S) && charges > 0)
// rb.AddForce(Vector3.down * Imp, ForceMode.Impulse);
//}
}
}
Answer by MosoWare · May 20, 2016 at 03:21 PM
You need to change the value of charges every time a charge is used.
if (Input.GetKey(KeyCode.Q) && charges > 0)
{
rb.AddForce(Vector3.left * Imp, ForceMode.Impulse);
charges--;
}
if (Input.GetKey(KeyCode.E) && charges > 0)
{
rb.AddForce(Vector3.right * Imp, ForceMode.Impulse);
charges--;
}
Lol. I'm feeling stupid. $$anonymous$$ade camera, made controller skeleton, made particles, and forgot to decrease. Need. $$anonymous$$ore. Sleep.
Your answer
Follow this Question
Related Questions
Use of if(Component) 1 Answer
Execution order of conditional operators in an if-statement 1 Answer
Locking player's Y position while jumping and near a wall 0 Answers
Im trying to make gameObject move only if specific button is pressed 1 Answer
Unity Update is ignoring the GetKey part of my statement!? 2 Answers