我正在关注这个Unity教程:
https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/moving-player?playlist=17141
虽然对于独立(Windows)平台来说球的速度很好,但使用Android时用户输入滚球的速度非常快。
这是独立的Windows用户输入,可以正常使用并作为我的Android模型:
/// <summary>
/// Gets all user input from a standalone device.
/// </summary>
private void GetInput_PC()
{
// Update the movement vector
m_movementVector.x = Input.GetAxis("Horizontal");
m_movementVector.y = 0.0f;
m_movementVector.z = Input.GetAxis("Vertical");
// Add force to the player's rigid body.
m_rigidBody.AddForce(m_movementVector * Speed * Time.deltaTime);
}
现在教程没有介绍的是如何在移动设备上实现这一点。这是我的尝试:
/// <summary>
/// Gets all user input from an Android device.
/// </summary>
private void GetInput_Android()
{
// Update the movement vector
m_movementVector.x = Input.GetAxis("Mouse X");
m_movementVector.y = 0.0f;
m_movementVector.z = Input.GetAxis("Mouse Y");
// Get the touch count
if (Input.touchCount > 0)
{
// Update the movement vector
m_movementVector.x += Input.touches[0].deltaPosition.x;
m_movementVector.y = 0.0f;
m_movementVector.z += Input.touches[0].deltaPosition.y;
// Add force to the player's rigid body.
m_rigidBody.AddForce(m_movementVector * Speed * Time.deltaTime);
}
}
然而,有一些问题:
1)球比独立实施移动得快得多。
2)父亲你的拇指移动,球越快。
总体而言,如何重新安排此Android代码与独立PC版本具有相同的速度?
添加完整脚本:
#region Using Clauses
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#endregion
public class PlayerController : MonoBehaviour
{
private Rigidbody m_rigidBody;
private Vector3 m_movementVector = new Vector3();
public float Speed;
/// <summary>
/// Called after initialization.
/// </summary>
private void Start()
{
m_rigidBody = GetComponent<Rigidbody>();
}
/// <summary>
/// Called before rending a frame.
/// </summary>
private void Update()
{
}
/// <summary>
/// Called before performing any physics calculations.
/// </summary>
private void FixedUpdate()
{
#if UNITY_EDITOR || UNITY_STANDALONE
GetInput_PC();
#elif UNITY_ANDROID
GetInput_Android();
#endif
}
/// <summary>
/// Gets all user input from a standalone device.
/// </summary>
private void GetInput_PC()
{
// Update the movement vector
m_movementVector.x = Input.GetAxis("Horizontal");
m_movementVector.y = 0.0f;
m_movementVector.z = Input.GetAxis("Vertical");
// Add force to the player's rigid body.
m_rigidBody.AddForce(m_movementVector * Speed * Time.deltaTime);
}
/// <summary>
/// Gets all user input from an Android device.
/// </summary>
private void GetInput_Android()
{
// Update the movement vector
m_movementVector.x = Input.GetAxis("Mouse X");
m_movementVector.y = 0.0f;
m_movementVector.z = Input.GetAxis("Mouse Y");
// Get the touch count
if (Input.touchCount > 0)
{
// Update the movement vector
m_movementVector.x += Input.touches[0].deltaPosition.x;
m_movementVector.y = 0.0f;
m_movementVector.z += Input.touches[0].deltaPosition.y;
// Add force to the player's rigid body.
m_rigidBody.AddForce(m_movementVector * Speed * Time.deltaTime);
}
}
}
第二次尝试,但是当拇指快速移动时仍然太快。
/// <summary>
/// Gets all user input from an Android device.
/// </summary>
private void GetInput_Android()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector3 movement = new Vector3(touchDeltaPosition.x, 0.0f, touchDeltaPosition.y);
m_rigidBody.AddForce(movement * Speed * Time.deltaTime);
}
}
编辑:尝试了第一个答案。正常化工作时,我仍然遇到Android和Windows之间出现不同速度的问题。根据多个页面和个人尝试,尝试使用建议的CrossPlatformInput不适用于Android。还在寻找答案。
答案 0 :(得分:2)
不确定为什么使用+=
代替=
来代替GetInput_Android
函数中的代码。即使问题并非如此,因为您在函数开头使用Input.GetAxis
覆盖了值,可能是当您删除Input.GetAxis("Mouse X")
和Input.GetAxis("Mouse Y")
时,{&1;}在GetInput_Android
函数内部。
问题是,您的Input.GetAxis("Horizontal")
和Input.GetAxis("Vertical")
在编辑器和0
以及{{1}上的1
和Input.touches.deltaPosition.x
之间返回值根据手指在屏幕上的速度返回值。有时,此值最高可达Input.touches.deltaPosition.y
,而且数量很大。
在使用它之前必须将其标准化:
42
或使用if (Input.touchCount > 0)
{
//Update the movement vector
m_movementVector.x = Input.touches[0].deltaPosition.x;
m_movementVector.y = 0.0f;
m_movementVector.z = Input.touches[0].deltaPosition.y;
//NORMALIZE VECTOR
m_movementVector.Normalize();
// Add force to the player's rigid body.
m_rigidBody.AddForce(m_movementVector * Speed * Time.deltaTime);
}
结构的标准化属性:
Vector3
虽然这可以解决问题,但这不是实现此问题的最佳方法。最好的方法是使用//Update the movement vector
m_movementVector.x = Input.touches[0].deltaPosition.normalized.x;
m_movementVector.y = 0.0f;
m_movementVector.z = Input.touches[0].deltaPosition.normalized.y;
OnBeginDrag
和OnDrag
函数制作虚拟操纵杆,这将适用于任何平台。您也可以使用已经制作过的&#34; OnEndDrag
&#34;在Assetstore上节省时间:
CrossPlatformInputManager