Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity Android Touch.position,can';别动_Android_Unity3d_Unityscript - Fatal编程技术网

Unity Android Touch.position,can';别动

Unity Android Touch.position,can';别动,android,unity3d,unityscript,Android,Unity3d,Unityscript,嘿,我是javascript和Unity的新手。我正在制作我的第一个应用程序,我正在尝试在我的android手机上使用touch.position。我就是不知道如何让我的角色(一个叫Bal的球)左、右、上、下移动。也无法让Bal跳跃 这是我的密码: #pragma strict var rotationSpeed = 100; var jumpHeight = 5; //(x,y,w,h) pixel coords. var rectButton1 = Rect(-0.12, 0.13, 76

嘿,我是javascript和Unity的新手。我正在制作我的第一个应用程序,我正在尝试在我的android手机上使用touch.position。我就是不知道如何让我的角色(一个叫Bal的球)左、右、上、下移动。也无法让Bal跳跃

这是我的密码:

#pragma strict

var rotationSpeed = 100;
var jumpHeight = 5;
//(x,y,w,h) pixel coords.
var rectButton1 = Rect(-0.12, 0.13, 76.1, 96.61);
var rectButton2 = Rect(0.21, 0.13, 76.1, 96.61);
var rectButton3 = Rect(0.81, 0.26, 15, 58);
private var isFalling = false;

function Update () {
    for (var touch : Touch in Input.touches) {
    if (rectButton1.Contains(touch.position)) {
            var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
            rotation *= Time.deltaTime;
            rigidbody.AddRelativeTorque (Vector3.left * rotation);
    } else if (rectButton2.Contains(touch.position)) {
            var rotation2 : float = Input.GetAxis ("Horizontal") * rotationSpeed;
            rotation2 *= Time.deltaTime;
            rigidbody.AddRelativeTorque (Vector3.right * rotation2);
    } else if (rectButton3.Contains(touch.position) && isFalling == false) {
            rigidbody.velocity.y = jumpHeight;
    }
            isFalling = true;
}
}

//No infinite jump!
function OnCollisionStay ()
{
isFalling = false;
}

在Unity中,触摸位置和鼠标位置被视为左下角的原点,而GUI组件被视为左上角的原点。要调整此差值,必须从Screen.height中减去当前位置

Vector2 touchPos=Vector2(touch.position.x,Screen.height touch.position.y)

i、 在你的情况下

for (var touch : Touch in Input.touches) {
Vector2 touchPos = Vector2(touch.position.x, Screen.height-touch.position.y);
if (rectButton1.Contains(touchPos)) {
        var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
        rotation *= Time.deltaTime;
        rigidbody.AddRelativeTorque (Vector3.left * rotation);
} else if (rectButton2.Contains(touchPos)) {
        var rotation2 : float = Input.GetAxis ("Horizontal") * rotationSpeed;
        rotation2 *= Time.deltaTime;
        rigidbody.AddRelativeTorque (Vector3.right * rotation2);
} else if (rectButton3.Contains(touchPos) && isFalling == false) {
        rigidbody.velocity.y = jumpHeight;
}
}

来自游戏发明者的脚本以恒定的速度将对象移动到触摸位置,而不考虑两点之间的距离。。您可以根据需要进行修改

using UnityEngine;
using System.Collections;

public class TapToMove : MonoBehaviour {
//flag to check if the user has tapped / clicked. 
//Set to true on click. Reset to false on reaching destination
private bool flag = false;
//destination point
private Vector3 endPoint;
//alter this to change the speed of the movement of player / gameobject
public float duration = 50.0f;
//vertical position of the gameobject
private float yAxis;

void Start(){
    //save the y axis value of gameobject
    yAxis = gameObject.transform.position.y;
}

// Update is called once per frame
void Update () {

    //check if the screen is touched / clicked   
    if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
    {
        //declare a variable of RaycastHit struct
        RaycastHit hit;
        //Create a Ray on the tapped / clicked position
        Ray ray;
        //for unity editor
        #if UNITY_EDITOR
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //for touch device
        #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
        ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
        #endif

        //Check if the ray hits any collider
        if(Physics.Raycast(ray,out hit))
        {
            //set a flag to indicate to move the gameobject
            flag = true;
            //save the click / tap position
            endPoint = hit.point;
            //as we do not want to change the y axis value based on touch position, reset it to original y axis value
            endPoint.y = yAxis;
            Debug.Log(endPoint);
        }

    }
    //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
    if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
        //move the gameobject to the desired position
        gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
    }
    //set the movement indicator flag to false if the endPoint and current gameobject position are equal
    else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
        flag = false;
        Debug.Log("I am here");
    }

    }
    }
你可以在下面的链接中找到整篇文章

您是否尝试过调试以查看touch.position返回的内容?在那里贴一个Debug.LogWarning(touch.position),这样你就可以得到一些反馈。我需要使用Android SDK模拟器吗?如果是这样的话,我就无法让它正常工作。不,当你在Unity编辑器中运行它时,它会在屏幕底部显示调试消息(红色表示错误,黄色表示警告)。我让球或我的角色在屏幕上移动,但它不会悲伤地旋转。。。我最终使用了一个标准资产(移动)scriptsok。。但请记住触摸屏的来源和GUI的来源是不同的。我用标准的assest mobile软件包解决了这个问题!谢谢你们的帮助!