- Home /
Question by
MichaelTheGreat · Nov 19, 2014 at 04:21 PM ·
shoot
Shooing and moving code
I am trying to move AND shoot (one bullet at a time) but what ends up happening is that I do either or, not both.
using UnityEngine;
using System.Collections;
public class shootingscript : MonoBehaviour
{
public GUITexture guiShoot;
public GUITexture guiLeft;
public GUITexture guiRight;
bool buttonShoot = false;
bool buttonLeft = false;
bool buttonRight = false;
void ReadButtons ()
{
if (Input.touchCount > 0)
{
// Get the touch info
Touch t = Input.GetTouch(0);
// Did the touch action just begin?
if (t.phase == TouchPhase.Began) {
if(guiShoot.HitTest (t.position))
Button ();
if (guiLeft.HitTest (t.position))
buttonLeft = true;
if (guiRight.HitTest (t.position))
buttonRight = true;
}
// Did the touch end?
if (t.phase == TouchPhase.Ended) {
buttonLeft = false;
buttonRight = false;
buttonShoot = false;
}
}
}
void Update()
{
ReadButtons ();
if (buttonRight){
transform.Translate(Vector2.right * 4f *Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (buttonLeft){
transform.Translate(Vector2.right * 4f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
}
void Button ()
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
weapon.Attack(false);
}
}
}
Comment
Please format the code by having consistent indentation if you want it to be taken seriously
Answer by richyrich · Nov 19, 2014 at 08:51 PM
Try this:
if (Input.touchCount > 0)
{
// Get the touch info
foreach (Touch t in Input.touches)
{
// Did the touch action just begin?
if (t.phase == TouchPhase.Began)
{
if (guiShoot.HitTest(t.position))
Button();
if (guiLeft.HitTest(t.position))
buttonLeft = true;
if (guiRight.HitTest(t.position))
buttonRight = true;
}
// Did the touch end?
if (t.phase == TouchPhase.Ended)
{
buttonLeft = false;
buttonRight = false;
buttonShoot = false;
}
}
}
Your answer
Follow this Question
Related Questions
moving gun in first person shooter while walking 8 Answers
shooting script wont work 3 Answers
How to shoot only once 5 Answers
How to spawn an object with an added force 1 Answer
Increased projectile velocity when holding button down 2D 1 Answer