使用角色控制器使角色跟随鼠标
Make character follow mouse using Character Controller
我有一款 2D space 射击游戏(想像像 Chicken Invaders 之类的游戏)已在 iOS 和 Android 上发布。我现在正在尝试将它移植到 WebGL,为此,我正在尝试使用鼠标实现移动。之前在移动平台上使用 Unity 的内置角色控制器成功实现了移动,我想继续在 WebGL 构建中使用它,以保持代码库小,并尽可能少地使用特定于平台的实现。
那么,应该用什么方法让飞船跟随鼠标光标呢?
PS:
在移动设备上,我使用 touch.deltaPosition 和 characterController.Move() 让船跟随手指的移动。
编辑:当前方法
输入管理器
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");
}
}
}
播放器控制器
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 方法更有效。
我有一款 2D space 射击游戏(想像像 Chicken Invaders 之类的游戏)已在 iOS 和 Android 上发布。我现在正在尝试将它移植到 WebGL,为此,我正在尝试使用鼠标实现移动。之前在移动平台上使用 Unity 的内置角色控制器成功实现了移动,我想继续在 WebGL 构建中使用它,以保持代码库小,并尽可能少地使用特定于平台的实现。
那么,应该用什么方法让飞船跟随鼠标光标呢?
PS: 在移动设备上,我使用 touch.deltaPosition 和 characterController.Move() 让船跟随手指的移动。
编辑:当前方法
输入管理器
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");
}
}
}
播放器控制器
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 方法更有效。