![](/img/trans.png)
[英]Unity - Make character look at mouse with New input system/Character Controller
[英]Make character follow mouse using Character Controller
我有一个在iOS和Android上发布的2D太空射击游戏(想象像“鸡入侵者”之类的东西)。 我现在正尝试将其移植到WebGL,并且为此,我尝试使用鼠标来实现移动。 之前在移动平台上使用Unity的内置字符控制器成功实现了机芯,我想在WebGL构建中继续使用它,以使代码库更小,并尽可能减少针对特定平台的实现。
因此,我应该使用哪种方法使飞船跟随鼠标光标?
PS:在移动设备上,我使用了带有characterController.Move()的touch.deltaPosition来使飞船跟随手指的运动。
编辑:当前方法
输入管理器
public Vector2 GetInput () {
switch(type) {
case PlatformType.Computer:
if (Input.GetButtonDown("Jump") || Input.GetAxis("Fire") != 0f) {
thisDelegate.InputDidReceiveFireCommand();
}
if (!Input.mousePresent) {
temp.x = Input.GetAxis("Horizontal");
temp.y = Input.GetAxis("Vertical");
} else {
if (Input.GetAxis("Mouse X") != 0f || Input.GetAxis("Mouse Y") != 0f) {
temp.x = Input.GetAxis("Mouse X");
temp.y = Input.GetAxis("Mouse Y");
} else {
temp.x = Input.GetAxis("Horizontal");
temp.y = Input.GetAxis("Vertical");
}
}
}
PlayerController
void HandleInput()
{
mvm = InputController.GetInput();
if (GetComponent<InputManager>().type == InputManager.PlatformType.Mobile) {
mvm *= Time.deltaTime * initialVeloc / 10;
} else {
if (mvm != Vector3.zero) {
mvm -= transform.position;
}
}
}
void Update () {
characterController.Move((Vector2)mvm);
}
当前方法使字符抖动靠近屏幕中心。
我已经修改了脚本,现在使用绝对鼠标位置而不是增量位置。 现在是这样的:
输入管理器:
if (Input.GetAxis("Mouse X") != 0f || Input.GetAxis("Mouse Y") != 0f) {
temp = Input.mousePosition;
temp.z = 12.5f;
// Vector3 initialMov = mvm;
temp = Camera.main.ScreenToWorldPoint(temp);
temp -= transform.position;
}
控制者
mvm = InputController.GetInput();
mvm *= Time.deltaTime * initialVeloc;
characterController.Move((Vector2)mvm);
就CPU使用率而言,此方法按预期工作,并且比RayCasting方法更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.