我试图理解Unity制作的PlayerController的代码(生存射击教程)。我理解一切,除了原因,为什么他们写了FLOOR-POINT(鼠标指向的地方)和玩家(变换位置)。我试图只写出没有玩家位置及其工作的落地点。他们为什么写这个?也许有人通过了这个教程,可以帮助我吗?
Unity教程:https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character?playlist=17144
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed= 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength=100f;
void Awake()
{
floorMask = LayerMask.GetMask("Floor");
anim = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate()
{
float v = Input.GetAxisRaw ("Vertical");
float h = Input.GetAxisRaw ("Horizontal");
Move (h, v);
Turning ();
Animating(h,v);
}
void Move(float h,float v)
{
movement.Set (h,0f,v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position+movement);
}
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast(camRay,out floorHit,camRayLength,floorMask))
{
Vector3 playerToMouse = floorHit.point-transform.position ;********
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
void Animating(float h,float v)
{
bool wakling = h != 0f || v != 0f;
anim.SetBool("IsWalking",wakling);
}
}
答案 0 :(得分:2)
Raycasts需要一个方向,通常以(someVector - someOtherVector)给出。此减法的结果是一个新的向量,它将someVector作为原点。在您的情况下,从鼠标到播放器的相对向量可能永远不会改变,因此似乎不需要减法。但是,最好使用两个向量的减法作为方向。