Skip to content

Instantly share code, notes, and snippets.

@DB-009
Created February 6, 2017 06:54
Show Gist options
  • Save DB-009/af94c541baa9f7ce91ad9cb0184480d3 to your computer and use it in GitHub Desktop.
Save DB-009/af94c541baa9f7ce91ad9cb0184480d3 to your computer and use it in GitHub Desktop.
Source Code for frontStyle movement
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public GameStateManager gameStateManager;
public float playerSpd, fwdSpd, sidSpd, jumpHeight;
public bool isGrounded;
public Rigidbody rb;
// Use this for initialization
void Awake () {
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if(gameStateManager.gameState == GameStateManager.GameState.OnePlayer)
{
GetMovementInputs();
}
}
void FixedUpdate()
{
if (gameStateManager.gameState == GameStateManager.GameState.OnePlayer)
{
ApplyMovementInput();
}
}
public void GetMovementInputs()
{
fwdSpd = 0;
sidSpd = 0;
//foward and back(z axis)
if(Input.GetKey(KeyCode.W))
{
fwdSpd += playerSpd;
}
else if (Input.GetKey(KeyCode.S))
{
fwdSpd -= playerSpd;
}
//horizontal
if (Input.GetKey(KeyCode.D))
{
sidSpd += playerSpd;
}
else if (Input.GetKey(KeyCode.A))
{
sidSpd -= playerSpd;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(0, jumpHeight, 0, ForceMode.Impulse);
}
}
public void ApplyMovementInput()
{
rb.AddForce(new Vector3(sidSpd, 0, fwdSpd), ForceMode.Force);
}
public void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "ground")
{
isGrounded = true;
}
if (col.gameObject.tag == "Respawn")
{
PlayerRespawn();
}
}
public void OnCollisionExit(Collision col)
{
if (col.gameObject.tag == "ground")
{
isGrounded = false;
}
}
public void PlayerRespawn()
{
Vector3 initPos = gameStateManager.initSpawnPos;
this.transform.position = new Vector3(initPos.x, initPos.y, this.transform.position.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment