1.
摄像机成为胶囊的子物体,摄像机上放代码:
using UnityEngine;
using System.Collections;
public class Sj : MonoBehaviour {
public GameObject player; //前台拖入胶囊
Vector3 rot = new Vector3(0, 0, 0); //先定义一个Vectory3类型的变量rot(0,0,0)
public float speed; //这个是鼠标灵敏度
void Start() {
}
void Update() {
float MouseX = Input.GetAxis("Mouse X")*speed;
float MouseY = Input.GetAxis("Mouse Y")*speed;
rot.x = rot.x - MouseY;
rot.y = rot.y + MouseX;
rot.z = 0; //锁定摄像头移动的角度z轴,防止左右倾斜
transform.eulerAngles = rot; //所有方向设定好后,摄像头的角度=rot
player.transform.eulerAngles = new Vector3(0, rot.y, 0); //角色角度只能通过MouseX改变大小,也就是锁定rot.y
}
}
场景加上碰撞器
Player胶囊身上加上如下代码:
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
float MoveX = Input.GetAxis("Horizontal");
float MoveY = Input.GetAxis("Vertical");
transform.Translate(new Vector3(MoveX, 0, MoveY) *
Time.deltaTime * speed);
if (Input.GetButtonDown("Jump")) {
Rigidbody rig = GetComponent<Rigidbody>();
rig.AddForce(new Vector3(0, 0.5f, 0), ForceMode.Impulse);
}
}
}
这是用刚体做的
就可以移动了
2.给胶