Unity Android 触摸不按预期工作
Unity Android touch not working as expected
我正在使用这个触摸脚本在触摸屏幕时显示一些效果:
var ParticleA : GameObject;
//var bulletHole : GameObject;
function Update () {
var hit : RaycastHit;
// Use Screen.height because many functions (like this one) start in the bottom left of the screen, while MousePosition starts in the top left
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 200))
{
var newparA = Instantiate(ParticleA, hit.point, Quaternion.identity);
//var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
//Instantiate(bulletHole, hit.point, hitRotation);
Destroy(newparA, 12.0);
}
}
}
它不适用于 Android。触摸时,它只在中央屏幕显示效果,而不是我手指实际触摸的位置。
可能是什么问题?
您不想使用 Input.GetMouseButtonDown(0)
(那是鼠标...正如其名称所示)。
使用Input.touches
:http://docs.unity3d.com/Manual/MobileInput.html
试试这个
var ParticleA : GameObject;
function Update ()
{
var hit : RaycastHit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (transform.position, Vector3.forward,out hit ))
{
var newparA =Instantiate(ParticleA, hit.point,Quaternion.identity );
Destroy(newparA, 12.0);
}
你可以试试这个:
var ParticleA:游戏对象;
函数更新(){
var 命中:RaycastHit;
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray, hit, 200))
{
var newparA = Instantiate(ParticleA, hit.point, Quaternion.identity);
Destroy(newparA, 12.0);
}
}
}
}
此代码使用 C#。将此代码放入 UPDATE() 函数中。它将在 Unity 编辑器和 Android 平台上运行。
RaycastHit hit;
Ray ray;
public Camera hudCamera;
if (Application.platform == RuntimePlatform.WindowsEditor)
{
ray = hudCamera.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider == transform.collider)
{
// Here transform.collider is the collider of that gameobject on which you attach this script
// Your Rest of the Logic Here
}
}
}
}
// This Will work on Android Device ////////////////////////////////////////////////////////////////////////
else if(Application.platform == RuntimePlatform.Android)
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
ray = hudCamera.ScreenPointToRay(Input.GetTouch(i).position);
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider == transform.collider)
{
// Here transform.collider is the collider of that gameobject on which you attach this script
// Your Rest of the Logic Here
}
}
}
if (Input.GetTouch(i).phase == TouchPhase.Moved)
{
// Logic for finger move on screen
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
if (Input.GetTouch(i).fingerId == fingerId)
{
fingerId = -1;
// Logic when touch ends
}
}
}
}
}
我正在使用这个触摸脚本在触摸屏幕时显示一些效果:
var ParticleA : GameObject;
//var bulletHole : GameObject;
function Update () {
var hit : RaycastHit;
// Use Screen.height because many functions (like this one) start in the bottom left of the screen, while MousePosition starts in the top left
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 200))
{
var newparA = Instantiate(ParticleA, hit.point, Quaternion.identity);
//var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
//Instantiate(bulletHole, hit.point, hitRotation);
Destroy(newparA, 12.0);
}
}
}
它不适用于 Android。触摸时,它只在中央屏幕显示效果,而不是我手指实际触摸的位置。
可能是什么问题?
您不想使用 Input.GetMouseButtonDown(0)
(那是鼠标...正如其名称所示)。
使用Input.touches
:http://docs.unity3d.com/Manual/MobileInput.html
试试这个
var ParticleA : GameObject;
function Update ()
{
var hit : RaycastHit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (transform.position, Vector3.forward,out hit ))
{
var newparA =Instantiate(ParticleA, hit.point,Quaternion.identity );
Destroy(newparA, 12.0);
}
你可以试试这个:
var ParticleA:游戏对象;
函数更新(){
var 命中:RaycastHit;
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray, hit, 200))
{
var newparA = Instantiate(ParticleA, hit.point, Quaternion.identity);
Destroy(newparA, 12.0);
}
}
}
}
此代码使用 C#。将此代码放入 UPDATE() 函数中。它将在 Unity 编辑器和 Android 平台上运行。
RaycastHit hit;
Ray ray;
public Camera hudCamera;
if (Application.platform == RuntimePlatform.WindowsEditor)
{
ray = hudCamera.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider == transform.collider)
{
// Here transform.collider is the collider of that gameobject on which you attach this script
// Your Rest of the Logic Here
}
}
}
}
// This Will work on Android Device ////////////////////////////////////////////////////////////////////////
else if(Application.platform == RuntimePlatform.Android)
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
ray = hudCamera.ScreenPointToRay(Input.GetTouch(i).position);
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider == transform.collider)
{
// Here transform.collider is the collider of that gameobject on which you attach this script
// Your Rest of the Logic Here
}
}
}
if (Input.GetTouch(i).phase == TouchPhase.Moved)
{
// Logic for finger move on screen
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
if (Input.GetTouch(i).fingerId == fingerId)
{
fingerId = -1;
// Logic when touch ends
}
}
}
}
}