C#Unity–3D迷宫
作者心得
2d迷宫都有了
能少的了3d???
这不就来了?
源代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Transform m_camTransform;
Vector3 m_camRot;
float m_camHeight = 1.4f;
public Transform m_transform;
CharacterController m_ch;
float m_movSpeed = 3.0f;
float m_gravity = 2.0f;
public int m_life = 5;
// Start is called before the first frame update
void Start()
{
m_transform = this.transform;
m_ch = this.GetComponent<CharacterController>();
m_camTransform = Camera.main.transform;
m_camTransform.position = m_transform.TransformPoint(0, m_camHeight,0);
m_camTransform.rotation = m_transform.rotation;
m_camRot = m_camTransform.eulerAngles;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
if (m_life <= 0)
return;
Control();
}
void Control()
{
float rh = Input.GetAxis("Mouse X");
float rv = Input.GetAxis("Mouse Y");
m_camRot.x -= rv;
m_camRot.y += rh;
m_camTransform.eulerAngles = m_camRot;
Vector3 camrot = m_camTransform.eulerAngles;
camrot.x = 0;camrot.z = 0;
m_transform.eulerAngles = camrot;
float xm = 0, ym = 0, zm = 0;
//ym -= m_gravity * Time.deltaTime;
if (Input.GetKey(KeyCode.W))
{
zm += m_movSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
zm -= m_movSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
xm -= m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))
{
xm += m_movSpeed * Time.deltaTime;
}
m_ch.Move(m_transform.TransformDirection(new Vector3(xm, ym, zm)));
m_camTransform.position = m_transform.TransformPoint(0,m_camHeight, 0);
}
}
效果截图
作者的话
迷宫算是unity的入门
毕竟都是我自学,也没打算研究太深
另外,我记得最后unity弄了一个大型3d跑酷的项目
为此,还特意去学了建模
那个项目奔跑,跳跃,捡道具,玩法较多
可惜,毕竟年代久远加换了n次系统,现在貌似找不到了
可惜!