Low poly style player joystick controller setup

November 5, 2019

 

 using System.Colle ctions;
using System.Collections.Generic;
using UnityEngine;

public class PS3Joystick : MonoBehaviour
{

 


    float h, v, hr, v;

    public  float speed = 2f;
    public float RotateSpeed = 500f;
    Rigidbody myRigid;
    public int shootingSpeed = 1000;
    public int jump = 100;
    // Start is called before the first frame update
    void Start()
    {
        myRigid = GetComponent<Rigidbody>();
       

    // Update is called once per frame
    void Update()
    {
        //Joystick left right.
        h = Input.GetAxis("Horizontal");
        transform.Translate(h * speed*Time.deltaTime, 0, 0);
  
        //Joystick up and down.
        v = Input.GetAxis("Vertical");
        transform.Translate(0, 0, v * speed * Time.deltaTime);

        if (Input.GetMouseButton(0))
        {
            transform.Rotate(0, hr * RotateSpeed*-10, 0);
        }

        if (Input.GetMouseButton(1))
        {
            transform.Rotate(0, hr * RotateSpeed * 10, 0);
        }

        //Right joystick rotation.
        //Input, Mouse ScrollWheel, Type to JoyStick, Axis to 3rd axis(Jotsticks and Scrollwheel).
        hr = Input.GetAxis("Mouse ScrollWheel");
       transform.Rotate(0, hr * RotateSpeed * Time.deltaTime, 0);


        //Joystick Y button(Joystick1 button0) it adds force.
        if (Input.GetKeyDown(KeyCode.Joystick1Button0) ||
            Input.GetKeyDown(KeyCode.E))
        {
            //transform.Translate(0, 0, 10f*Time.deltaTime);
            //myRigid.AddForce(Vector3.forward*Time.deltaTime);
            myRigid.AddRelativeForce(0, 0, 10*shootingSpeed);// local direction.
        }

        //Joystick A button (Joystick1 button2) -Z local moving.
        if (Input.GetKeyDown(KeyCode.Joystick1Button2)|| Input.GetKeyDown(KeyCode.R))
        {
            myRigid.AddRelativeForce(0, 0, -10 * shootingSpeed);
        }

        //Joystick B button, respawn to world 0 location.
        if (Input.GetKeyDown(KeyCode.Joystick1Button1))
        {
            myRigid.velocity = Vector3.zero;// removes Addforce.
            transform.position = new Vector3 (0,1,0);
        }


        if (Input.GetKeyDown(KeyCode.Joystick1Button2) || Input.GetKeyDown(KeyCode.Space))
        {
            myRigid.AddForce(0, jump, 0);
        }

    }

}
 

Please reload

Featured Posts

Simple Instantiate code snippet

June 21, 2015

1/2
Please reload

Recent Posts
Please reload

Archive
Please reload