- Home /
Code Logic Help??
Hello, I am having this problem with my code logic. When right click is pressed, I want the weapon the character is holding to aim; but the problem is when right click is pressed instead of aiming the current weapon another weapon appear.
Can anybody help me come up with a better code logic or with this problem?
Thanks,
public GameObject primaryWeapon;
public GameObject secondaryWeapon;
private bool drawPistol = false;
private bool drawRifle = false;
private bool isAimPistol = false;
private bool isAimRifle = false;
private Animator animator;
void Start () {
animator = GetComponent<Animator>();
}
void Update () {
DrawPistol ();
DrawRifle ();
AimingP ();
AimingR ();
}
public void DrawPistol(){
if(Input.GetKeyUp(KeyCode.Alpha1) && !drawPistol){
PistolDraw();
}
else if(Input.GetKeyUp(KeyCode.Alpha1) && drawPistol){
unDrawPistol();
}
}
public void PistolDraw()
{
drawPistol = true;
animator.SetBool ("DrawPistol", true);
secondaryWeapon.SetActive(true);
animator.SetBool ("DrawRifle", false);
primaryWeapon.SetActive(false);
}
public void unDrawPistol()
{
drawPistol = false;
animator.SetBool ("DrawPistol", false);
secondaryWeapon.SetActive(false);
}
public void DrawRifle()
{if(Input.GetKeyUp(KeyCode.Alpha2) && !drawRifle){
RifleDraw();
}
else if(Input.GetKeyUp(KeyCode.Alpha1) && drawRifle){
unDrawRifle();
}
}
public void RifleDraw(){
drawRifle = true;
animator.SetBool ("DrawRifle", true);
animator.SetBool ("DrawPistol", false);
primaryWeapon.SetActive(true);
}
public void unDrawRifle()
{
drawRifle = false;
animator.SetBool ("DrawRifle", false);
primaryWeapon.SetActive(false);
}
public void AimingP()
{
if(Input.GetKey(KeyCode.Mouse1) && !isAimPistol){
AimPistol();
}
else if(Input.GetKey(KeyCode.Mouse1) && isAimPistol){
unAimPistol();
}
}
public void AimPistol(){
isAimPistol = true;
animator.SetBool ("AimPistol", true);
secondaryWeapon.SetActive (true);
}
public void unAimPistol()
{
isAimPistol = false;
animator.SetBool ("AimPistol", false);
}
public void AimingR()
{
if(Input.GetKey(KeyCode.Mouse1) && !isAimRifle){
AimRifle();
isAimPistol = false;
}
else if(Input.GetKey(KeyCode.Mouse1) && isAimRifle){
unAimRifle();
}
}
public void AimRifle(){
isAimRifle = true;
animator.SetBool ("AimRifle", true);
primaryWeapon.SetActive (true);
}
public void unAimRifle()
{
isAimRifle = false;
animator.SetBool ("AimRifle", false);
}
Answer by NoseKills · Dec 20, 2014 at 02:34 PM
In short i think the main problem is that you are doing AimPistol
and AimRifle
every update no matter which weapon you have drawn - so no matter the selected weapon, both models are enabled when you click. You should call only one of those methods depending on drawRifle
and drawPistol
.
As a general advice I'd suggest you aim toward a more generic logic. The way you are doing things now, you have to write all these methods again and rethink your if()s when ever you implement a third, fourth, etc. weapon. This is because you have one class handling all the input, logic and animation.
You should aim toward a solution where each weapon has a script attached to it that knows the specifics of drawing or aiming that weapon. Then you'd have a script like this one that just changes the weapon based on number keys and calls the Aim and Draw methods with info from the weapon script.
To do this properly you would need to use inheritance. Might sound intimidating if you haven't done it before but this would be a very nice situation to use and learn it :)
In "pseudo code"... you'd do something like this.
public class YourClass : MonoBehaviour {
// keep all keycodes that are used for drawing weapons here
private KeyCode[] weaponButtons = new KeyCode[]{KeyCode.Alpha1, KeyCode.Alpha2};
// drag the weapon GameObjects here in an order that matches keys in the above array
public WeaponScript[] weapons;
// variable pointing to the currently draw weapon (script)
private WeaponScript currentWeapon;
// aiming or not
private bool aiming = false;
private Animator animator;
void Start () {
animator = GetComponent<Animator>();
}
void Update ()
{
if (Input.anyKeyDown()) // if any key was pressed
{
int i = 0;
// check each alpha key
while (i < weaponButtons.Length && i < weapons.Length)
{
// if that alpha key was pressed
if (Input.GetKeyDown(weaponButtons[i]))
{
// and if the weapon corresponding to that key is not already drawn
if (weapons[i] != currentWeapon)
{
DrawNewWeapon(weapons[i]); // draw that weapon
break; // exit the loop because we are already drawing a weapon
}
}
i++;
}
}
if (Input.GetKeyDown(KeyCode.Mouse1) && currentWeapon != null)
{
// weapon drawn and clicking mouse
// aim or unaim
ToggleAiming();
}
}
public void DrawNewWeapon(WeaponScript newWeapon)
{
if (currentWeapon != null)
{
animator.SetBool(currentWeapon.GetDrawBoolName(), false);
currentWeapon.gameObject.SetActive(false);
}
// set aiming and drawn state to animator
animator.SetBool(newWeapon.GetAimBoolName(), aiming);
animator.SetBool(newWeapon.GetDrawBoolName(), true);
newWeapon.gameObject.SetActive(false);
}
public void ToggleAiming()
{
aiming = !aiming;
animator.SetBool(newWeapon.GetAimBoolName(), aiming);
}
}
// Make a script that's common to all weapons
// all weapons must know what are their aim and draw animations' names
public abstract class WeaponScript : MonoBehaviour
{
// abstract methods are methods that child classes must implment
public abstract string GetDrawBoolName();
public abstract string GetAimBoolName();
}
// the actual weapon script you attach to your Pistol
// extends WeaponScript so compiler knows it has the 2 needed methods
public class PistolScript : WeaponScript {
public override string GetDrawBoolName(){
return "DrawPistol";
}
public override string GetAimBoolName(){
return "AimPistol";
}
}
// the actual weapon script you attach to your Rifle
// extends WeaponScript so compiler knows it has the 2 needed methods
public class RifleScript : WeaponScript {
public override string GetDrawBoolName(){
return "DrawRifle";
}
public override string GetAimBoolName(){
return "AimRifle";
}
}
If you manage to do something like this, your code works as well with 100 weapons as it does with 2. All you have to do is make another WeaponScript. Also the drawing and aiming logic is now the same for all weapons so if there are bugs in them, you have only a few lines of code to debug and search mistakes in.
Thanks for the code logic, it helps. But the problem now is that the weapon object is showing when drawn. Am I suppose to set it up in the WeaponScript?
Thanks,
Accept his answer if its the solution you used!
Follow up questions should be asked separately. We do not encourage walkthrough Do-It-For-mes.
I hastily wrote the code so I spotted at least one mistake there
newWeapon.gameObject.SetActive(false);
// should of course be
newWeapon.gameObject.SetActive(true);
probably other problems too...
Your answer
Follow this Question
Related Questions
Mouse flying 0 Answers
How to make a 2Dgun shoot at the position of mouse? 3 Answers
Character Not Rotating 0 Answers
mouse dissapear 1 Answer
Get Mouse Position GUI 3 Answers