简体   繁体   中英

Android Controller for Unity game

I was making game in Unity 3D and used the one click converter of unity to convert it in Android .apk

The game is opening in Android phone but the player is not moving

Player controller Script:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public Vector2 moving = new Vector2();
    public int Bulletlimit = 0;
    public int MaxBulletlimit = 3;
    public bool Gun;
    private float lastShotTime ;
    public float fireDelay = 0.2f;
    public Transform BulletDirection;

    public Bullet bullet;

    // Use this for initialization
    void Start () {
        lastShotTime = Time.time;
    }

    // Update is called once per frame
    void Update () {


        moving.x = moving.y = 0;

        if (Input.GetKey ("right")) {
            moving.x = 1;
        } else if (Input.GetKey ("left")) {
            moving.x = -1;
        }

        if (Input.GetKey ("up")) {
            moving.y = 1;
        } else if (Input.GetKey ("down")) {
            moving.y = -1;
        }


        if (Input.GetKey ("s")) {

            if(Gun){
            if(Bulletlimit < MaxBulletlimit)
            {

                    if(Time.time > lastShotTime + fireDelay)
                    {
                        Bullet clone = Instantiate (bullet, BulletDirection.position, Quaternion.identity) as Bullet;
                    Bulletlimit = Bulletlimit + 1;
                        lastShotTime = Time.time;
                    }
                }
            }
    }   

    }

    public void BulletCount()
    {
        Bulletlimit = Bulletlimit - 1;
    }          
}

How do I make him move in touch screens?

Your code is based on keystrokes - that (probably) wont apply to your touch screen.

There are a few ways you could do this, for something so simple I would probably try adding a Canvas along with some buttons to act as controls.

From there you can use the OnMouseDown()/OnMouseUp()/OnClick() methods (This also converts to touchscreens) instead of keystrokes.

How you code from there is a choice you have to make but in this case I would likely use the buttons to turn on/off movement bools and check/apply them in the Update() method.

If you're unsure of how to use the new unity UI try this...

https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas

If you're running the new version of unity (currently 5.1 I believe), my editor looks different to the tutorials and won't stay as default for some reason. Simply set the editor/inspector view to default if some of the options appear to be missing.

There are more complex things you can do with the actual touch inputs but I don't think you need to worry about it in this particular case.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM