Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having many problems with this. I am a new game developer and I am using a Unity tutorial. But I want to add touch input for movement and a touch button for firing the weapon. How can I do this? I followed several tutorials and even the documentation provided by Unity, and could not get it to work. Here is my code.

For Player Movement and animation:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 100f;   
    void Awake()
    {
        floorMask = LayerMask.GetMask ("Floor");
        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();
    }
    void FixedUpdate()
    {
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");
        Move (h, v);
        Turning ();
        Animating (h, v);
    }
    void Move (float h, float v)
    {
        movement.Set (h, 0f, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition (transform.position + movement);
    }
    void Turning ()
    {
        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit floorHit;
        if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
        {
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;
            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
            playerRigidbody.MoveRotation (newRotation);
        }
    }
    void Animating (float h, float v)
    {
        bool walking = h != 0f || v !=0f;
        anim.SetBool ("IsWalking", walking);
    }
}

For firing the weapon:

using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
    public int damagePerShot = 20;
    public float timeBetweenBullets = 0.15f;
    public float range = 100f;


    float timer;
    Ray shootRay;
    RaycastHit shootHit;
    int shootableMask;
    ParticleSystem gunParticles;
    LineRenderer gunLine;
    AudioSource gunAudio;
    Light gunLight;
    float effectsDisplayTime = 0.2f;


    void Awake ()
    {
        shootableMask = LayerMask.GetMask ("Shootable");
        gunParticles = GetComponent<ParticleSystem> ();
        gunLine = GetComponent <LineRenderer> ();
        gunAudio = GetComponent<AudioSource> ();
        gunLight = GetComponent<Light> ();
    }


    void Update ()
    {
        timer += Time.deltaTime;


        if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
        {
            Shoot ();
        }

        if(timer >= timeBetweenBullets * effectsDisplayTime)
        {
            DisableEffects ();
        }
    }


    public void DisableEffects ()
    {
        gunLine.enabled = false;
        gunLight.enabled = false;
    }


    void Shoot ()
    {
        timer = 0f;

        gunAudio.Play ();

        gunLight.enabled = true;

        gunParticles.Stop ();
        gunParticles.Play ();

        gunLine.enabled = true;
        gunLine.SetPosition (0, transform.position);

        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;

        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
            if(enemyHealth != null)
            {
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }
            gunLine.SetPosition (1, shootHit.point);
        }
        else
        {
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }
}

Any help would be greatly appreciated :)

share|improve this question
    
What isn't working? What is it doing instead? You will need to elaborate more. – Savlon Jan 26 '15 at 0:24
    
Movement for touch is not working at all. I have tried several tutorials and didnt get it to work. I am just asking how can I get it to work or what code should i put in. I am very new to this so I dont really know how to add it in at all. (Even tried unity's tutorial and it didnt work) – MJonesDev Jan 26 '15 at 0:27
    
You may want to start off with some basic unity tutorials because it seems as though you are trying to implement something out of your knowledge range... – Savlon Jan 26 '15 at 6:39
    
I have tried tutorials before and movement and control is in my knowledge. I am just asking for touch specifically. Everything else is in my knowledge. I just cant get touch working for some reason. – MJonesDev Jan 26 '15 at 16:42
    
I have written code for touch in an earlier tutorial and it worked, but this one does not. I am just asking for some examples of code that can lead me in the roght direction to this. – MJonesDev Jan 26 '15 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.