文章目录
unity使用操作技巧
Unity镜头视角
个人推荐,鼠标右键然后键盘wasd键控制视角
public属性和private属性的区别
public CharacterController characterController;
private CharacterController characterController;
public(不推荐)
可以直接面板拖拽完成属性的赋值(但是不推荐,打包项目时容易丢失属性)
private(推荐)
从Assets文件夹被移除的资源是可以被还原的
节点绑定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EmptyTest : MonoBehaviour
{
public GameObject Cube;
// Start is called before the first frame update
void Start()
{
//立方体的名称
Debug.Log(Cube.name);
}
// Update is called once per frame
void Update()
{
}
}
注意图层覆盖
大的覆盖小的,bg覆盖了role
背景覆盖人的操作方式
注意两个背景一个player以及Layer的值,大的覆盖小的,下的覆盖上面的
游戏分辨率设置
pakage Manager资源移除
它会显示所有已购买资源,如果不想让他显示,只能通过“资源商城中隐藏”
项目重命名
角色移动
角色移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoveController : MonoBehaviour
{
private CharacterController characterController;
public float speed = 10f; //移动速度
public Vector3 moveDirection; //设置移动方向
// Start is called before the first frame update
void Start()
{
//获取player身上的CharacterController组件
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Move();
}
public void Move()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
moveDirection = (transform.right * horizontal + transform.forward * vertical).normalized; //设置玩家移动方向
characterController.Move(moveDirection * speed * Time.deltaTime);
}
}
相机视角跟随角色
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotation : MonoBehaviour
{
public float mouseSensitivity = 1000f; //实现灵敏度
public Transform playerBody; //玩家位置
public float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY; //将上下旋转的轴值进行累加
xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
}
}
相机转向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotation : MonoBehaviour
{
public float mouseSensitivity = 1000f; //实现灵敏度
public Transform playerBody; //玩家位置
public float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY; //将上下旋转的轴值进行累加
xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
}
}
对齐视图
让game与Scene视图展示效果一样
FPS游戏
角色移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerMoveComment : MonoBehaviour
{
private CharacterController characterController;
public float speed = 10f; //移动速度
public Vector3 moveDirection; //设置移动方向
// Start is called before the first frame update
void Start()
{
//获取player身上的CharacterController组件
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Move();
}
public void Move()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
moveDirection = (transform.right * horizontal + transform.forward * vertical).normalized; //设置玩家移动方向
characterController.Move(moveDirection * speed * Time.deltaTime);
}
}
视角
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///摄像机的旋转
/// 玩家左右旋转控制实现左右旋转
/// 摄像机上下旋转控制实现上下旋转
/// </summary>
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 1000f; //实现灵敏度
public Transform playerBody; //玩家位置
public float xRotation = 0f;
private void Start()
{
//将光标锁定在该游戏窗口的中心,隐藏硬件光标
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY; //将上下旋转的轴值进行累加
xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
}
}
鼠标控制镜头旋转
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///摄像机的旋转
/// 玩家左右旋转控制实现左右旋转
/// 摄像机上下旋转控制实现上下旋转
/// </summary>
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 1000f; //实现灵敏度
public Transform playerBody; //玩家位置
public float xRotation = 0f;
private void Start()
{
//将光标锁定在该游戏窗口的中心,隐藏硬件光标
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY; //将上下旋转的轴值进行累加
xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
}
}
RPG游戏
人物移动
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class MaleController : MonoBehaviour
{
//玩家面对的对象,IK
public Transform target;
//animator组件
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//向量
Vector3 dir = new Vector3(horizontal,0,vertical);
AnimatorStateInfo stateinfo = animator.GetCurrentAnimatorStateInfo(0);
if (dir != Vector3.zero && !stateinfo.IsName("pickup") && !stateinfo.IsName("wave"))
{
//面向向量 quaternion是一个四元数的类,生成的四元数给旋转
transform.rotation = Quaternion.LookRotation(dir);
//播放跑步动画
animator.SetBool("isRun",true);
//使角色朝向前方移动
transform.Translate(Vector3.forward * 2 * Time.deltaTime);
}
else
{
animator.SetBool("isRun",false);
}
//判断点击键盘F键时触发pickup
if (Input.GetKeyDown(KeyCode.F))
{
animator.SetTrigger("pickup");
}
if (Input.GetKeyDown(KeyCode.G))
{
animator.SetTrigger("wave");
}
}
//IK写道这个方法内
private void OnAnimatorIK(int layerIndex)
{
//设置头部IK权重
animator.SetLookAtWeight(1);
//使头部看向这个位置
animator.SetLookAtPosition(target.position);
//设置右手IK权重
// animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
// //旋转权重
// animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
// //设置右手IK
// animator.SetIKPosition(AvatarIKGoal.RightHand,target.position);
// animator.SetIKRotation(AvatarIKGoal.RightHand,target.rotation);
}
}
碰撞器触发器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class PlayerColliderDesk : MonoBehaviour
{
private VideoPlayer video;
//监听发生碰撞
// private void OnCollisionEnter(Collision collsion)
// {
// Debug.Log(collsion.collider.name);
// GameObject Plane_Video = GameObject.Find("Plane_Video");
// video = Plane_Video.GetComponent<VideoPlayer>();
// if (video.isPlaying && collsion.collider.name.Equals("TV_furniture"))
// {
// Debug.Log("暂停");
// video.Pause();
// }
// }
//
// //持续碰撞中
// private void OnCollisionStay(Collision collsion){
//
//
// }
//
// //碰撞结束
// private void OnCollisionExit(Collision collsion)
// {
// if (!video.isPlaying & collsion.collider.name.Equals("TV_furniture"))
// {
// Debug.Log("开始");
// video.Play();
// }
// }
//触发开始
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.name);
Debug.Log("触发器执行");
GameObject Plane_Video = GameObject.Find("Plane_Video");
video = Plane_Video.GetComponent<VideoPlayer>();
if (video.isPlaying && other.name.Equals("TV_furniture"))
{
Debug.Log("暂停");
video.Pause();
}
}
//正在触发
private void OnTriggerStay(Collider other)
{
}
//触发结束
private void OnTriggerExit(Collider other)
{
if (!video.isPlaying && other.name.Equals("TV_furniture"))
{
Debug.Log("开始");
// video.Play();
}
}
}