【问题标题】:Moving a GameObject inside canvas to touchpoint将画布内的游戏对象移动到接触点
【发布时间】:2016-11-16 04:04:23
【问题描述】:

我是 Unity 3D 的新手,并且正在按照 Udemy 教程学习该平台。 我在课程中开发了一个游戏 GoHippoGo,其中河马的图像在屏幕上移动到触摸点。

虽然当我尝试使用画布和面板等...使游戏适合所有屏幕尺寸时,河马停止了移动!触摸时它只会从屏幕上掉下来(太慢了),而不是移动到触摸点。

我尝试搜索整个互联网,甚至是统一论坛,但找不到解决方案。 我的错误可能很明显,但由于我是新手,请合作:P

谢谢

这是我画布的截图:

这是我的 MoveHippo 脚本:

using UnityEngine;
using System.Collections;

public class MoveHippo : MonoBehaviour {

    private float lastTouchTime, currentTouchTime;

    public float velocityVal;
    public float torqueVal;
    public float thresholdTime;


    void Awake() {
        velocityVal = 8.0f;
        torqueVal = 200.0f;
        thresholdTime = 0.3f;
    }

    void Update () {

    #if UNITY_ANDROID
        moveHippoAndroid ();
    #endif

    #if UNITY_EDITOR
        moveHippo();
    #endif  
    }

    void moveHippo() { //For testing only in your COMPUTER
        Vector3 currentPos, touchedPos, distanceVec;
        if (Input.GetMouseButtonDown(0)) {
            startRotatingHippoAndStopIt();
        }

        else if (Input.GetMouseButtonUp(0)) {
            currentPos = Camera.main.WorldToScreenPoint (transform.position);
            touchedPos = Input.mousePosition;
            distanceVec = (touchedPos - currentPos).normalized;
            stopRotatingHippoAndMoveIt(distanceVec, velocityVal);
        }
    }

    void moveHippoAndroid() { 
        Vector3 currentPos, touchedPos, distanceVec;        
        for (int i = 0; i < Input.touches.Length; i++) {
            Touch touch = Input.GetTouch(i);
            currentPos = Camera.main.WorldToScreenPoint(transform.position);
            touchedPos = touch.position;
            distanceVec = (touchedPos - currentPos).normalized;
            if (Input.GetTouch(0).phase == TouchPhase.Began) {
                startRotatingHippoAndStopIt();
            } else if (Input.GetTouch(0).phase == TouchPhase.Ended){
                currentTouchTime = Time.time;
                if (currentTouchTime - lastTouchTime > thresholdTime) { //No Double Touch detected ...
                    lastTouchTime = Time.time;
                    stopRotatingHippoAndMoveIt(distanceVec, velocityVal);
                } else if (currentTouchTime - lastTouchTime < thresholdTime){ //Double Touch detected!
                    lastTouchTime = Time.time;
                    stopRotatingHippoAndMoveIt(distanceVec, velocityVal*2.0f);
                }
            }
        }
    }

    void startRotatingHippoAndStopIt() {
        // We rorate the hippo...
        GetComponent<Rigidbody2D>().fixedAngle = false;
        GetComponent<Rigidbody2D>().AddTorque(torqueVal);

        // ... and stop it
        GetComponent<Rigidbody2D>().velocity=Vector2.zero;
    }

    void stopRotatingHippoAndMoveIt(Vector3 distanceVec, float velocity) {
        // We stop rotating the hippo...
        Quaternion hippoQuatern = new Quaternion();
        hippoQuatern.eulerAngles = new Vector3(0,0,0);
        GetComponent<Rigidbody2D>().fixedAngle = true;
        GetComponent<Rigidbody2D>().transform.rotation = hippoQuatern;

        // ... and move it.
        GetComponent<Rigidbody2D>().velocity = distanceVec*velocity;
    }

}

【问题讨论】:

  • MoveHippo 脚本附加到什么上?相机还是河马?
  • MoveHippo 脚本已附加到 Hippo

标签: android canvas unity3d gameobject


【解决方案1】:

尝试使用(RectTransform)transform.positionGetComponent&lt;RectTransform&gt;().position(或简单的transform.position,但我想记住我处理的是RectTransform,而不是标准的Transform)而不是 Camera.main.WorldToScreenPoint(transform.position) :由于您的 Canvas 设置为 Screen Space - OverlayRectTransform 组件的全局位置将返回 X,像素坐标中的 Y,Z 值。

如果您的屏幕是 800x600 并且您的对象居中,GetComponent&lt;RectTransform&gt;().position 将返回 (400.0f, 300.0f, 0.0f)。

【讨论】:

  • 我按照你说的做了,现在河马正在向接触点移动,但是非常非常非常缓慢。速度设置为 8.0f,但似乎永远无法到达目的地。
  • 我知道问题出在哪里,整个场景视图只有 1 个单位。当我记录 distanceVec 时,它显示 x 的值,y 坐标 b/w 0-1,z 为 0。我不知道如何解决这个问题。
  • @ayuschjain 场景视图并不是真正的 1 个单位:distanceVec 值介于 -1 和 1 之间,因为矢量是标准化的(也是 distanceVec.z= 0 因为您在使用 Canvas 时处于 2D 状态:z 与深度“相关”)。如果您想增加河马的速度,只需增加 velocityVal(类似于 100.0f)。如果你想实现一种慢速制动,你必须为 Rigidbody2D.Linear Drag 属性设置一个值(类似于 0.5f)。
  • 当我按照 Udemy 教程进行操作时,即。在不使用画布的情况下,distanceVec 值不在 b/w -1 到 1 之间,它大于那个值。向量也在那里被标准化。我什至试图增加 velocityVal ,但没有用.....:(
  • @ayuschjain Uhm... 我觉得有些不对劲:如果你对向量进行归一化,它的长度(大小)将变为 1,因此它的任何分量都不能低于 -1 且大于 1 (查看here 以获得Vector3.normalized)信息。此外,我使用我在之前评论中给出的值对 Rigidbody2D 建议的更改尝试了您的脚本,并且效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多