原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1017/91.html
如果让大家去实现游戏中角色的左右前后移动甚至旋转功能对于大家来说肯定不难,但对于摄像机的实现可能有些初学者就犯难了,下面常州网站开发与游戏开发工作室幻天网络就来带大家看看摄像机的左右前后移动以及旋转实现,其核心方式就是就是摄像机跟随角色移动。
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour
{
CharacterController controller;
public Vector3 moveDirection = Vector3.zero;
public float moveSpeed = 5f;
public float rotateSpeed;
public float x, fx, y, fy;
public float gravity = 200.0f;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
//transform.eulerAngles = Vector3.zero;
}
// Update is called once per frame
void FixedUpdate()
{
rotateSpeed = GlobalSetting.MouseSensitive;
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
moveDirection = new Vector3(h, 0, v);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if (Input.GetMouseButton(1))
{
x -= Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
fx = -x;
fy = -y;
transform.eulerAngles = new Vector3(-fy, fx, 0);
}
}
}
using UnityEngine;
using System.Collections;
//存入
public class GlobalSetting
{
private static GlobalSetting Instance;
public static float MouseSensitive = 150f;
public static float voice = 0.5f;
//确保该实例只有1个
public static GlobalSetting GetInstance()
{
if (Instance == null)
{
Instance = new GlobalSetting();
}
return Instance;
}
}
上篇:上一篇:Unity3D GameView-BaseWindow
下篇:下一篇:Unity3d倒计时实现