共计 5626 个字符,预计需要花费 15 分钟才能阅读完成。
Unity 游戏光明之光笔记
第三章 创立游戏运行场景及角色管制
- 创立场景导入资源等
- 标签治理
应用标签来进行断定,应用 const 缩小代码的出错率
// 高空标签
public const string ground = "Ground";//const 表明这个是一个共有的不可变的变量
// 玩家标签
public const string player = "Player";
- 鼠标管制人物挪动
鼠标点击高空特效,在角色上挂载脚本脚本
public GameObject effect_click_prefab;
void Update()
{
// 检测鼠标的按下
// 将鼠标的坐标转换为射线该
// 射线与高空产生碰撞返回产生碰撞的点,而后让角色转向该点,开始挪动。当挪动到肯定范畴时进行挪动。if (Input.GetMouseButtonDown(0))
{Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
// 保留碰撞信息
RaycastHit hitInfo;
bool isCollider = Physics.Raycast(ray, out hitInfo);
if(isCollider&& hitInfo.collider.tag ==Tags.ground)
{
// 实例化点击的成果
showClickEffect(hitInfo.point);
}
}
void showClickEffect(Vector3 hitPoint)
{hitPoint = new Vector3(hitPoint.x, hitPoint.y+0.1f,hitPoint.z);
GameObject.Instantiate(effect_click_prefab, hitPoint, Quaternion.identity);
}
}
管制配角的朝向(鼠标点击高空的时候转向)
减少两个对象
private Vector3 targetPosition = Vector3.zero;
private bool isMoving = false;// 示意鼠标是否按下
在 update 办法的 if(isCollider&& hitInfo.collider.tag ==Tags.ground)中减少
isMoving = true;
LookAtTarget(hitInfo.point);
在 update 办法中减少两个断定语句
if (Input.GetMouseButtonUp(0))
{isMoving = false;}
if (isMoving)
{
// 失去要挪动的指标地位
// 让配角朝向指标地位
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCollider = Physics.Raycast(ray, out hitInfo);
if (isCollider && hitInfo.collider.tag == Tags.ground)
{LookAtTarget(hitInfo.point);
}
}
减少一个 lookat 办法
void LookAtTarget(Vector3 hitPoint)
{
targetPosition = hitPoint;
targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
this.transform.LookAt(targetPosition);
}
管制配角的挪动,添 PlayerMove 脚本挂载到角色上
默认值是以后地位不会在游戏一开始时就运行在 PlayDir 脚本 Start 函数中增加
targetPosition = transform.position;
public float speed = 4;
private PlayerDir dir;
private CharacterController controller;
void Start()
{dir = this.GetComponent<PlayerDir>();
controller = this.GetComponent<CharacterController>();}
void Update()
{float distance = Vector3.Distance(dir.targetPosition, transform.position);
if (distance > 0.3f)
{
isMoving = true;
//state = PlayerState.Moving;
controller.SimpleMove(transform.forward * speed);
}
}
增加人物动画
管制人物动画播放的脚本
using UnityEngine;
using System.Collections;
public class PlayerAnimation : MonoBehaviour
{
private PlayerMove move;
void Start()
{move = this.GetComponent<PlayerMove>();
}
void LateUpdate()
{if (move.state == PlayerState.Moving)
{PlayAnim("Run");
}
else if (move.state == PlayerState.Idle)
{PlayAnim("Idle");
}
}
void PlayAnim(string animName)
{GetComponent<Animation>().CrossFade(animName);
}
}
留神在 void PlayAnim 办法中教程中应用的旧版的代码,新版的代码为
GetComponent<Animation>().CrossFade(animName);
欠缺的 PlayerDir 代码
public class PlayerDir : MonoBehaviour
{
public GameObject effect_click_prefab;
public Vector3 targetPosition = Vector3.zero;
private PlayerMove playerMove;
private bool isMoving = false;// 示意鼠标是否按下
private void Start()
{
// 默认值是以后地位不会在游戏一开始时就运行
targetPosition = transform.position;
playerMove = this.GetComponent<PlayerMove>();}
void Update()
{if (Input.GetMouseButtonDown(0))
{Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCollider = Physics.Raycast(ray, out hitInfo);
if (isCollider && hitInfo.collider.tag == Tags.ground)
{
isMoving = true;
ShowClickEffect(hitInfo.point);
LookAtTarget(hitInfo.point);
}
}
if (Input.GetMouseButtonUp(0))
{isMoving = false;}
if (isMoving)
{
// 失去要挪动的指标地位
// 让配角朝向指标地位
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCollider = Physics.Raycast(ray, out hitInfo);
if (isCollider && hitInfo.collider.tag == Tags.ground)
{LookAtTarget(hitInfo.point);
}
}
else {if (playerMove.isMoving)
{LookAtTarget(targetPosition);
}
}
}
// 实例化进去点击的成果
void ShowClickEffect(Vector3 hitPoint)
{hitPoint = new Vector3(hitPoint.x, hitPoint.y + 0.1f, hitPoint.z);
GameObject.Instantiate(effect_click_prefab, hitPoint, Quaternion.identity);
}
// 让配角朝向指标地位
void LookAtTarget(Vector3 hitPoint)
{
targetPosition = hitPoint;
targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
this.transform.LookAt(targetPosition);
}
}
- 管制相机追随配角的挪动
在主摄像机挂在 FollowCamera 脚本
private Transform player;
private Vector3 offsetPosition;// 地位偏移
void Start()
{player = GameObject.FindGameObjectWithTag(Tags.player).transform;
// 使相机永远朝向配角
transform.LookAt(player.position);
offsetPosition = transform.position - player.position;
}
void Update()
{transform.position = offsetPosition + player.position;}
应用鼠标滑动管制相机视线的拉近和拉远
定义一个 public float scrollSpeed = 10;
在下面的 Update 办法中减少 ScrollView();办法
void ScrollView()
{//print(Input.GetAxis("Mouse ScrollWheel"));// 向后 返回负值 (拉近视线) 向前滑动 返回正值(拉远视线)
distance = offsetPosition.magnitude;
// 获取鼠标中键的值
distance += Input.GetAxis("Mouse ScrollWheel") *- scrollSpeed;
distance = Mathf.Clamp(distance, 2, 18);
offsetPosition = offsetPosition.normalized * distance;
}
留神要在 distance += Input.GetAxis(“Mouse ScrollWheel”) *- scrollSpeed; 中的
- scrollSpeed 负号,才是与实际效果统一
相机间隔的限度
distance = Mathf.Clamp(distance, 2, 18);
相机的旋转
void RotateView()
{//Input.GetAxis("Mouse X");// 失去鼠标在程度方向的滑动
//Input.GetAxis("Mouse Y");// 失去鼠标在垂直方向的滑动
if (Input.GetMouseButtonDown(1))
{isRotating = true;}
if (Input.GetMouseButtonUp(1))
{isRotating = false;}
if (isRotating)
{transform.RotateAround(player.position, player.up, rotateSpeed * Input.GetAxis("Mouse X"));
Vector3 originalPos = transform.position;
Quaternion originalRotation = transform.rotation;
transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));// 影响的属性有两个 一个是 position 一个是 rotation
float x = transform.eulerAngles.x;
if (x < 10 || x > 80)
{// 当超出范围之后,咱们将属性归位原来的,就是让旋转有效
transform.position = originalPos;
transform.rotation = originalRotation;
}
}
offsetPosition = transform.position - player.position;
}
留神方向函数的符号,还有 update 办法中调用的程序
限定旋转的方向
float x = transform.eulerAngles.x;
if (x < 10 || x > 80)
{// 当超出范围之后,咱们将属性归位原来的,就是让旋转有效
transform.position = originalPos;
transform.rotation = originalRotation;
}
- 减少角色状态信息
给角色增加 playerstatus 脚本
public int grade = 1;
public int hp = 100;
public int mp = 100;
public int coin = 200;// 金币数量
public int attack = 20;
public int attack_plus = 0;
public int def = 20;
public int def_plus = 0;
public int speed = 20;
public int speed_plus = 0;
public int point_remain = 0;// 残余的点数
【游戏开发】
正文完