第一人称控制器
using UnityEngine;
using System.Collections;
using System;
public class Move : MonoBehaviour {
Transform head;
public float speed = 10f;
Rigidbody self;
public float rotateSpeed = 10f;
// Use this for initialization
void Start () {
self = gameObject.GetComponent<Rigidbody>(); //物体自带钢体 这里获得钢体组件
head = GameObject.Find("Head").transform;
}
// Update is called once per frame
void Update () {
MoveH();
rotate();
}
private void rotate()
{
float MouseX = Input.GetAxis("Mouse X");
float MouseY = Input.GetAxis("Mouse Y");
transform.Rotate(Vector3.up * MouseX * rotateSpeed);
head.Rotate(-Vector3.right * MouseY * rotateSpeed); //注意负号
}
void MoveH()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 v = (transform.forward * vertical + transform.right *horizontal ) * speed * Time.deltaTime;
self.velocity = v; //钢体速度避免穿墙
}
}