我要提炼出我的这个漫游视角代码,这该怎么提炼呀
有好心人士帮忙解答吗
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
public class Move : MonoBehaviour
{
// Start is called before the first frame update
public int ThisType;
public GameObject Road;
public int ThisRoad;
public GameObject[] Cam;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (ThisType == 0)
{
if (Input.GetKey(KeyCode.W))
{
this.transform.Translate(Vector3.forward * Time.deltaTime * 3);
this.GetComponent<Animator>().SetInteger("State", 1);
}
else if (Input.GetKey(KeyCode.S))
{
this.transform.Translate(-Vector3.forward * Time.deltaTime * 3);
this.GetComponent<Animator>().SetInteger("State", 2);
}
else
{
this.GetComponent<Animator>().SetInteger("State", 0);
}
if (Input.GetKey(KeyCode.A))
{
this.transform.Rotate(-Vector3.up * Time.deltaTime * 50);
//this.GetComponent<Animator>().SetInteger("State", 1);
}
else if (Input.GetKey(KeyCode.D))
{
this.transform.Rotate(Vector3.up * Time.deltaTime * 50);
// this.GetComponent<Animator>().SetInteger("State", 2);
}
this.GetComponent<NavMeshAgent>().enabled = false;
}
else
{
this.GetComponent<Animator>().SetInteger("State", 1);
this.GetComponent<NavMeshAgent>().enabled = true;
this.GetComponent<NavMeshAgent>().SetDestination(Road.transform.GetChild(ThisRoad).position);
if (Vector3.Distance(this.transform.position, Road.transform.GetChild(ThisRoad).position) <= 2)
{
ThisRoad += 1;
if (ThisRoad >= Road.transform.childCount)
{
ThisRoad = 0;
}
}
}
if (Input.GetKeyDown(KeyCode.Q))
{
if (ThisType == 0)
{
ThisType = 1;
}
else
{
ThisType = 0;
}
}
if (Input.GetKeyDown(KeyCode.E))
{
if (Cam[0].activeSelf)
{
Cam[0].SetActive(false);
Cam[1].SetActive(true);
}
else
{
Cam[1].SetActive(false);
Cam[0].SetActive(true);
}
}
}
}
针对你的问题结合chatgpt知识库请参考以下内容:
这段代码主要实现了一个漫游视角的控制功能,可以实现游戏角色在场景中随意移动,并且可以切换不同的视角。下面是对这个代码的提炼:
执行 Start() 方法
该方法中并没有执行任何操作,为空。
执行 Update() 方法
该方法定义了游戏角色在场景中的移动方式和切换视角的方法,并根据玩家输入的按键来响应不同的操作。
具体操作如下:
代码中还包括一些注释,可以作为代码的解释和说明。
代码如下:
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Move : MonoBehaviour
{
public int ThisType; // 控制运动类型的变量
public GameObject Road; // 道路对象
public int ThisRoad; // 当前所在道路的编号
public GameObject[] Cam; // 摄像机数组
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// 手动控制角色移动
if (ThisType == 0)
{
if (Input.GetKey(KeyCode.W))
{
this.transform.Translate(Vector3.forward * Time.deltaTime * 3);
this.GetComponent<Animator>().SetInteger("State", 1);
}
else if (Input.GetKey(KeyCode.S))
{
this.transform.Translate(-Vector3.forward * Time.deltaTime * 3);
this.GetComponent<Animator>().SetInteger("State", 2);
}
else
{
this.GetComponent<Animator>().SetInteger("State", 0);
}
if (Input.GetKey(KeyCode.A))
{
this.transform.Rotate(-Vector3.up * Time.deltaTime * 50);
}
else if (Input.GetKey(KeyCode.D))
{
this.transform.Rotate(Vector3.up * Time.deltaTime * 50);
}
this.GetComponent<NavMeshAgent>().enabled = false;
}
// 自动控制角色移动
else
{
this.GetComponent<Animator>().SetInteger("State", 1);
this.GetComponent<NavMeshAgent>().enabled = true;
this.GetComponent<NavMeshAgent>().SetDestination(Road.transform.GetChild(ThisRoad).position);
if (Vector3.Distance(this.transform.position, Road.transform.GetChild(ThisRoad).position) <= 2)
{
ThisRoad += 1;
if (ThisRoad >= Road.transform.childCount)
{
ThisRoad = 0;
}
}
}
// 切换控制模式
if (Input.GetKeyDown(KeyCode.Q))
{
ThisType = 1 - ThisType;
}
// 切换摄像机模式
if (Input.GetKeyDown(KeyCode.E))
{
Cam[0].SetActive(!Cam[0].activeSelf);
Cam[1].SetActive(!Cam[1].activeSelf);
}
}
}
提炼后的代码更加简洁明了,易于阅读和理解。