【问题标题】:unity moving an object to right/left side统一将对象移动到右侧/左侧
【发布时间】:2016-09-28 20:56:07
【问题描述】:

我是 Unity 的新手,我正在尝试做一个简单的任务:触摸一个对象,然后释放你的触摸。当您释放时,我想检查您在屏幕的哪一侧释放了触摸,然后将对象移动到屏幕的那一侧。 因此,如果我按下我的对象,然后将我的手指滑动到右侧,对象将向左移动,右侧相同...

这是我的代码,附加到游戏对象上,由于某种原因,该对象只是移动到屏幕的右侧。即使我使用了Lerp,它也会立即执行。

void OnMouseUp()
{
    Vector3 pos = Input.mousePosition;

    Debug.Log("press off"); 

    if (pos.x < Screen.width / 2)
    {
        transform.position = Vector3.Lerp(transform.position, new Vector3(0,0,0), 2f * Time.deltaTime);
    }
    else
    {
        transform.position = Vector3.Lerp(transform.position, new Vector3(Screen.width, 0, 0), 2f * Time.deltaTime);
    }
}

谢谢!

【问题讨论】:

    标签: unity3d touch


    【解决方案1】:

    所以在我尝试了很多之后:

    public float smoothing = 7f;
    
        IEnumerator MoveCoroutine(Vector3 target)
        {
            while (Vector3.Distance(transform.position, target) > 0.05f)
            {
                transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
    
                yield return null;
            }
        }
    
        void OnMouseUp()
        {
            Plane p = new Plane(Camera.main.transform.forward, transform.position);
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
            float d;
            if (p.Raycast(r, out d))
            {
                Vector3 target = r.GetPoint(d);
            if (target.x > 0)
            {
                Debug.Log("right:" + target.x + " total: " + Screen.width);
                target.x = 5;
                target.y = 0;
            }
            else
            {
                Debug.Log("left:" + target.x + " total: " + Screen.width);
                target.x = -5;
                target.y = 0;
            }
    
                StartCoroutine(MoveCoroutine(target));
            }
        }
    

    不确定 Ray 投射的作用,如果有人能解释一下,我会很高兴。

    【解决方案2】:

    您的代码几乎是正确的。您只需要定义目标位置并在更新函数中每次调用 Lerp。

    一个简单的解决方案是定义两个空对象作为位置目标,并将它们作为参数传递给函数。

    using UnityEngine;
    using System.Collections;
    
    public class ClickTest : MonoBehaviour {
        public Transform posLeft;
        public Transform posRight;
        private Vector3 destPos;
    
        void Setup()
        {
            // default target is init position
            destPos = transform.position;
        }
    
        // Update is called once per frame
        void Update () {
            // Define target position
            if (Input.GetMouseButtonUp (0)) {
                Vector3 pos = Input.mousePosition;
                Debug.Log("press off : "+pos+ " scren : "+Screen.width); 
    
                if (pos.x < Screen.width / 2)
                {
                    Debug.Log("left");
                    destPos = posLeft.position;
                }
                else
                {
                    Debug.Log("right");
                    destPos = posRight.position;
                }
            }
            // update position to target
            transform.position =  Vector3.Lerp(transform.position, destPos, 2f * Time.deltaTime);
        }
    }
    

    Screenshot with empty objects as parameters

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多