using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Collections;
public class Player : MonoBehaviour {
//获取角色控制器
public CharacterController playerCC;
//摄像机
private Camera Mcamera;
//移动速度
public float speed = 10f;
//跳跃距离
public float vy = 0;
//重力
public float groayt = -2;
//摄像机高度
public float cmcmHeight = 0.5f;
// Use this for initialization
void Start () {
//获取主摄像机(角色摄像机)
Mcamera = Camera.main;
}
// Update is called once per frame
void Update () {
//调用Move()函数
Move();
//调用CameraControl()函数
CameraControl();
}
/// <summary>
/// 移动函数
/// </summary>
private void Move()
{
//轴输入
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (!playerCC.isGrounded)
{
vy += groayt * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.Space)) {
vy = 2;
}
//利用角色控制器进行移动
playerCC.Move(playerCC.transform.TransformDirection(new Vector3(h, vy, v)speedTime.deltaTime));
}
//摄像机及角色旋转
private void CameraControl()
{
//创建一个欧拉角,并赋值为主摄像机的欧拉角
Vector3 cmEuler = Mcamera.transform.eulerAngles;
//时刻记录鼠标滑动的值,并赋值给角色
cmEuler.x -= Input.GetAxis("Mouse Y");
cmEuler.y += Input.GetAxis("Mouse X");
//并将角色的欧拉角赋值给摄像机
Mcamera.transform.eulerAngles = cmEuler;
//只让角色的y轴发生旋转
cmEuler.x = cmEuler.z = 0;
transform.eulerAngles = cmEuler;
//让摄像机跟随角色
Mcamera.transform.position = this.transform.position + new Vector3(0, cmcmHeight, 0);
}
}
clipboard.png clipboard (1).png clipboard (2).png clipboard (3).png
网友评论