* -----英雄的移动控制
*
*
*
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class HeroMovingControl : MonoBehaviour
{
public float FloHeroMovingSpeed = 1F; //运动的速度
public float _FloHeroGaravity=1F; //英雄的重力 private CharacterController _ChaHeroControl; //英雄角色控制器
private Vector3 _VecHeroMoving; //英雄移动 private Transform _TranCamera; //摄像机的方位
private Vector3 _VecCameraRotation; //摄像机旋转 // Use this for initialization
void Start () {
//得到角色控制器
_ChaHeroControl = this.GetComponent<CharacterController>();
//得到摄像机的方位
_TranCamera = Camera.main.transform;
} // Update is called once per frame
void Update()
{
/* 英雄的旋转*/
//摄像机旋转
float FloX = Input.GetAxis("Mouse X");
float FloY = Input.GetAxis("Mouse Y");
_VecCameraRotation.y+= FloX;
_VecCameraRotation.x -= FloY;
_TranCamera.transform.eulerAngles = _VecCameraRotation;
//英雄的旋转
this.transform.eulerAngles = new Vector3(0, _VecCameraRotation.y,0); /* 英雄的移动 */
_VecHeroMoving = Vector3.zero;
//英雄的重力
_VecHeroMoving.y -= _FloHeroGaravity;
if (Input.GetKey(KeyCode.W))
{
_VecHeroMoving.z += FloHeroMovingSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.S))
{
_VecHeroMoving.z -= FloHeroMovingSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
_VecHeroMoving.x -= FloHeroMovingSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))
{
_VecHeroMoving.x += FloHeroMovingSpeed * Time.deltaTime;
}
//Move()方法必须使用世界坐标系
_ChaHeroControl.Move(this.transform.TransformDirection(_VecHeroMoving));
}
}