HAYT 15 июл 2018, 19:34
PlController:
using UnityEngine;
[RequireComponent (typeof (M_HeadBob))]
[RequireComponent (typeof (M_PlayerMotor))]
public class M_PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookspeed = 3f;
private M_PlayerMotor motor;
[SerializeField]
private float run_sp;
public bool runing;
[SerializeField]
private float walk_sp;
public bool walking;
public bool idlle;
public GameObject Camera;
private Vector3 camRotation;
private Vector3 movHor;
private Vector3 movVer;
public GameObject WP;
public WeaponSelect WPS;
private Vector3 velocity;
void Start() {
motor = GetComponent<M_PlayerMotor>();
Camera = GameObject.FindWithTag ("MainCamera");
WP = GameObject.FindWithTag ("WeaponSelect");
WPS = WP.GetComponent<WeaponSelect> ();
}
void Update()
{
float xMow = Input.GetAxisRaw("Horizontal");
float zMow = Input.GetAxisRaw("Vertical");
movHor = transform.right * xMow;
movVer = transform.forward * zMow;
velocity = (movHor + movVer).normalized * speed;
motor.Move(velocity);
float yRot = Input.GetAxisRaw("Mouse X");
Vector3 rotation = new Vector3(0f, yRot, 0f) * lookspeed;
motor.Rotate(rotation);
float xRot = Input.GetAxisRaw("Mouse Y");
camRotation = new Vector3(xRot, 0f, 0f) * lookspeed;
motor.RotateCam(camRotation);
}
void FixedUpdate(){
if (Input.GetKey (KeyCode.LeftShift) & Input.GetKey(KeyCode.W)) {
runing = true;
speed = run_sp;
walking = false;
} else {
speed = walk_sp;
runing = false;
}
if(!walking & !runing){
idlle = true;
}
if(Input.GetButtonDown("Jump")){
motor.Jump ();
}
if (movHor == Vector3.zero & movVer == Vector3.zero & motor.IsGrounded == true) {
idlle = true;
walking = false;
} else {
if(!runing){
walking = true;
}
}
if(walking){
if (WPS.Base.activeSelf == true) {
if (WPS.Base_name == "Shotgun") {
WPS.Base.GetComponent <ShotGun> ().MoveChange ();
} else {
WPS.BaseS.MoveChange ();
}
} else {
if (WPS.Pistol.activeSelf == true) {
WPS.PistolS.MoveChange ();
}
}
}
}
public void AddRecoil(float recoil){
Camera.transform.Rotate (-recoil, 0f, 0f);
}
}
/////////////////////////////////////////////////////////////////////////////////
PlMotor:
using System;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class M_PlayerMotor : MonoBehaviour
{
[SerializeField]
public Camera cam;
private Rigidbody rb;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 rotationCamera = Vector3.zero;
public bool IsGrounded;
public float CheckGroundDistance;
public float J_force;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Move(Vector3 _velocity) {
velocity = _velocity;
}
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
public void RotateCam(Vector3 _rotationCam)
{
rotationCamera = _rotationCam;
}
void FixedUpdate() {
PerformMove();
PerformRotate();
CheckGround ();
var rot_ = cam.transform.rotation.eulerAngles.y;
if (rot_ > 90){
rot_ = 90;
}
}
void PerformMove() {
if (velocity != Vector3.zero & IsGrounded) {
//
}
}
void PerformRotate () {
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if (cam != null)
cam.transform.Rotate (-rotationCamera);
}
void CheckGround(){
RaycastHit hit;
if (Physics.Raycast (transform.position, -transform.up, out hit, CheckGroundDistance)) {
IsGrounded = true;
} else {
IsGrounded = false;
}
}
public void Jump(){
if(IsGrounded){
rb.AddForce (transform.up * J_force);
}
}
}