94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class PlayerController : MonoBehaviour {
|
|
|
|
public Joystick JoyMove;
|
|
public Joystick JoyRotate;
|
|
|
|
public float moveSpeed = 1;
|
|
private float rotateLeftRight = 0;
|
|
private float rotateUpDown = 0;
|
|
private float rotateSpeed = 50.0f;
|
|
private GameObject cameraObject;
|
|
public Button jumpButton;
|
|
private CharacterController c;
|
|
public CrossHair crosshair;
|
|
private GameObject weaponSpot;
|
|
private Gun[] guns;
|
|
private Gun activeGun;
|
|
private GunStats stats;
|
|
private Vector3 speedVector = Vector3.zero;
|
|
public float jumpForce = 8.0f;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
cameraObject = gameObject.transform.FindChild ("Camera").gameObject;
|
|
rotateUpDown = transform.localEulerAngles.x;
|
|
rotateLeftRight = transform.localEulerAngles.y;
|
|
c = gameObject.GetComponent<CharacterController> ();
|
|
stats = gameObject.GetComponent<GunStats> ();
|
|
weaponSpot = Utils.FindChildRecursive (gameObject, "WeaponSpot");
|
|
guns = weaponSpot.GetComponentsInChildren<Gun> (true);
|
|
foreach (var gun in guns) {
|
|
gun.gameObject.SetActive(false);
|
|
}
|
|
SwitchGun (0);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
if (c.isGrounded) {
|
|
float angle = Mathf.Rad2Deg * JoyMove.Angle;
|
|
var forward = transform.forward;
|
|
Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.up);
|
|
speedVector = (rotation * forward);
|
|
speedVector.y = 0;
|
|
speedVector = speedVector.normalized * (JoyMove.Value * moveSpeed);
|
|
|
|
if(jumpButton.Pressed){
|
|
speedVector.y = jumpForce;
|
|
}
|
|
}
|
|
|
|
speedVector.y -= 9.8f * (Time.deltaTime);
|
|
c.Move (speedVector * Time.deltaTime);
|
|
|
|
if (JoyRotate != null && JoyRotate.Value != 0.0f) {
|
|
rotateLeftRight += JoyRotate.ValueX * (rotateSpeed * Time.deltaTime);
|
|
rotateUpDown += JoyRotate.ValueY * (rotateSpeed * Time.deltaTime);
|
|
rotateUpDown = Mathf.Clamp(rotateUpDown, -90.0f, 90.0f);
|
|
|
|
cameraObject.transform.localEulerAngles = new Vector3(rotateUpDown,0, 0);
|
|
transform.localEulerAngles = new Vector3(0,rotateLeftRight, 0);
|
|
}
|
|
|
|
}
|
|
|
|
public void SwitchGun(int id){
|
|
if (activeGun == guns[id]) {
|
|
return;
|
|
}
|
|
if (activeGun != null) {
|
|
activeGun.gameObject.SetActive(false);
|
|
}
|
|
|
|
activeGun = guns[id];
|
|
activeGun.gameObject.SetActive(true);
|
|
stats.SetGun (activeGun);
|
|
//Destroy (weaponSpot.transform.GetChild (0));
|
|
//guns
|
|
}
|
|
|
|
void OnDrawGizmos(){
|
|
if (c == null) {
|
|
c = gameObject.GetComponent<CharacterController> ();
|
|
}
|
|
if(c != null){
|
|
Gizmos.DrawWireCube (c.center + transform.position, new Vector3 (c.radius * 2, c.height, c.radius * 2));
|
|
}
|
|
|
|
}
|
|
}
|