Created
May 18, 2017 13:17
-
-
Save robbestad/4c0b447ae8994a194e8d3621503ec3f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Player : MonoBehaviour | |
{ | |
public float moveForce = 14f; | |
public float maxSpeed = 25f; | |
public float jumpForce = 0.5f; | |
[HideInInspector] | |
public bool facingRight = true; | |
[HideInInspector] | |
private Rigidbody player; | |
private bool jump; | |
private bool isJumping = false; | |
private bool canMoveLeft; | |
private bool canMoveRight; | |
private float hitDistance = 0.1f; | |
private float offset = 0.4f; | |
[SerializeField] | |
private float thrust = 14f; | |
public float groundDistance = 1.5f; | |
public float fallSpeed = 35f; | |
private RaycastHit hit; | |
private RaycastHit enemyHit; | |
private bool Xbox_One_Controller = false; | |
private bool PS4_Controller = false; | |
private float gravityConstant = 6.5f; | |
private int layerMask; | |
// Use this for initialization | |
void Awake() | |
{ | |
player = GetComponent<Rigidbody>(); | |
// Bit shift the index of the player layer and invert the bitmask | |
layerMask = 1 << 13; | |
layerMask = ~layerMask; | |
} | |
void FixedUpdate() | |
{ | |
jump = false; | |
if (player.velocity.y >= 0) | |
{ | |
// Does the ray intersect any objects excluding the player layer | |
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, groundDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.blue); | |
jump = true; | |
} | |
else if (Physics.Raycast(transform.position + transform.right * offset, transform.TransformDirection(Vector3.down), out hit, groundDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.blue); | |
jump = true; | |
} | |
else if (Physics.Raycast(transform.position - transform.right * offset, transform.TransformDirection(Vector3.down), out hit, groundDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.blue); | |
jump = true; | |
} | |
else | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * 2, Color.red); | |
jump = false; | |
} | |
} | |
Debug.DrawRay(transform.position + transform.up * offset, transform.TransformDirection(Vector3.left) * hitDistance, Color.red); | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hitDistance, Color.red); | |
Debug.DrawRay(transform.position - transform.up * offset, transform.TransformDirection(Vector3.left) * hitDistance, Color.red); | |
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.green); | |
canMoveLeft = false; | |
} | |
else if (Physics.Raycast(transform.position + transform.up * offset, transform.TransformDirection(Vector3.left), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.green); | |
canMoveLeft = false; | |
} | |
else if (Physics.Raycast(transform.position - transform.up * offset, transform.TransformDirection(Vector3.left), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.green); | |
canMoveLeft = false; | |
} | |
else | |
{ | |
canMoveLeft = true; | |
} | |
Debug.DrawRay(transform.position + transform.up * offset, transform.TransformDirection(Vector3.right) * hitDistance, Color.red); | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hitDistance, Color.red); | |
Debug.DrawRay(transform.position - transform.up * offset, transform.TransformDirection(Vector3.right) * hitDistance, Color.red); | |
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.green); | |
canMoveRight = false; | |
} | |
else if (Physics.Raycast(transform.position + transform.up * offset, transform.TransformDirection(Vector3.right), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.green); | |
canMoveRight = false; | |
} | |
else if (Physics.Raycast(transform.position - transform.up * offset, transform.TransformDirection(Vector3.right), out hit, hitDistance, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.green); | |
canMoveRight = false; | |
} | |
else | |
{ | |
canMoveRight = true; | |
} | |
// disable jumping if player is not grounded the y axis | |
if (player.velocity.y == 0) | |
{ | |
// isJumping = false; | |
} | |
/* XBOX CONTROLLER */ | |
string[] names = Input.GetJoystickNames(); | |
for (int x = 0; x < names.Length; x++) | |
{ | |
Debug.Log(names[x] + ", " + names[x].Length); | |
if (names[x].Length == 19) | |
{ | |
print("PS4 CONTROLLER IS CONNECTED"); | |
PS4_Controller = true; | |
Xbox_One_Controller = false; | |
} | |
if (names[x].Length == 33) | |
{ | |
print("XBOX ONE CONTROLLER IS CONNECTED"); | |
//set a controller bool to true | |
PS4_Controller = false; | |
Xbox_One_Controller = true; | |
} | |
} | |
if (Xbox_One_Controller) | |
{ | |
Debug.Log("X1 controls active"); | |
} | |
else if (PS4_Controller) | |
{ | |
//Debug.Log("PS3"); | |
//do something | |
} | |
else | |
{ | |
// there is no controllers | |
/* KEYBOARD CONTROLLER */ | |
float dir = Input.GetAxis("Horizontal"); | |
float restrictedForceInAir = 0.3f; | |
if (canMoveLeft & Input.GetKey("left")) | |
{ | |
//float force = dir; | |
//if (isJumping) force = -restrictedForceInAir; | |
//player.AddForce(force, 0, 0, ForceMode.Impulse); | |
Debug.Log("left"); | |
transform.position = new Vector3(transform.position.x - 0.2f, transform.position.y, transform.position.z); | |
} | |
else if (canMoveRight & Input.GetKey("right")) | |
{ | |
Debug.Log("right"); | |
//float force = dir; | |
//if (isJumping) force = restrictedForceInAir; | |
//player.AddForce(force, 0, 0, ForceMode.Impulse); | |
transform.position = new Vector3(transform.position.x + 0.2f, transform.position.y, transform.position.z); | |
} | |
if (Input.GetKey("space") & jump & !isJumping) | |
{ | |
isJumping = true; | |
//player.AddForce(0, jumpForce, 0, ForceMode.Impulse); | |
transform.position = new Vector3(transform.position.x, transform.position.y + jumpForce, transform.position.z); | |
} | |
} | |
if(isFloating()){ | |
float gravity = gravityConstant * Time.deltaTime; | |
Debug.Log("player is floating"); | |
isJumping = true; | |
transform.position = new Vector3(transform.position.x, transform.position.y - gravity, transform.position.z); | |
} else { | |
Debug.Log("player is NOT floating"); | |
//isJumping = false; | |
} | |
} | |
bool isFloating(){ | |
// Sink if the player is not on solid ground | |
RaycastHit objectDown; | |
bool floating = true; | |
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out objectDown, 0.5f, layerMask)) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * objectDown.distance, Color.red); | |
floating = false; | |
isJumping = true; | |
} | |
return floating; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment