Skip to content

Instantly share code, notes, and snippets.

@DB-009
Created April 8, 2019 23:27
Show Gist options
  • Save DB-009/3e915584a025dcb4cc856bdd18510270 to your computer and use it in GitHub Desktop.
Save DB-009/3e915584a025dcb4cc856bdd18510270 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControls : MonoBehaviour
{
public gunShooting _gunShooting; // class reference to gunShooting script connected to player
public float moveSpeed, jumpHeight; //player move speed and height of jumps
public bool isGrounded, canJump;//is player grounded and can he jump currently
public float zMovement, xMovement;//current input on Z and X axis
public bool isPlatformer;//is the game in platform mode
public Rigidbody _rb;
// Start is called before the first frame update
void Start()
{
_rb = this.GetComponent<Rigidbody>();//set the rigidbody to this players
}
// Update is called once per frame
void Update()
{
getInputs();//call getInputs function to detect inputs for movement
removeInputs();//reset movement back to zero unless holding keys down
if(Input.GetMouseButtonDown(0))//if player left clicks mouse shoot a bullet
{
_gunShooting.Shoot();//call Shoot function/methos in _gunShooting reference
}
}
private void FixedUpdate()
{
_rb.AddForce(new Vector3(xMovement*moveSpeed,0,zMovement*moveSpeed),ForceMode.Force );//apply force to player based of x*Z input variables
}
public void getInputs()
{
if(isPlatformer == false)
{
if (Input.GetKey(KeyCode.W))//get if players holding down W
{
//Debug.Log("Player moving up");
zMovement = 1;
}
else if (Input.GetKey(KeyCode.S))//get if players holding down S
{
//Debug.Log("Player moving down");
zMovement = -1;
}
}
if (Input.GetKey(KeyCode.A))//get if players holding down A
{
//Debug.Log("Player moving left");
xMovement = -1;
}
else if (Input.GetKey(KeyCode.D))//get if players holding down D
{
//Debug.Log("Player moving right");
xMovement = 1;
}
}
public void removeInputs()
{
if(isPlatformer == false)
{
if (Input.GetKeyUp(KeyCode.W))//get if players let go of W
{
//Debug.Log("Player moving up");
zMovement = 0;
}
else if (Input.GetKeyUp(KeyCode.S))//get if players let go of S
{
//Debug.Log("Player moving down");
zMovement = 0;
}
}
if (Input.GetKeyUp(KeyCode.A))//get if players let go of A
{
//Debug.Log("Player moving left");
xMovement = 0;
}
else if (Input.GetKeyUp(KeyCode.D))//get if players let go of D
{
//Debug.Log("Player moving right");
xMovement = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment