【发布时间】:2018-04-06 20:52:59
【问题描述】:
我正在尝试理解 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,0,0) 位置?
-
是的,我现在试过了,它仍然有效!
-
我认为是玩家面对鼠标所在的位置。例如,尝试进入一个角落,然后点击播放器的右后方。玩家将面向角落而不是鼠标位置