1
votes

I am new to unity and I am trying to make a simple task: touch an object and then release your touch. when you release, I want to check in which side of the screen you released your touch and then move the object to that side of the screen. So if I am pressing on my object, then swiping my finger to thr giht side, the object will move left and same for the right side...

This is my code, attached to the game object, and for some reason the object is just going to the right side of the screen. and it do it immidietly even though I used 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);
    }
}

thank you!

2

2 Answers

0
votes

So After a lot of trying this worked out for me:

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));
        }
    }

not sure what the Ray casting does, I would be glad if someone can explain.

0
votes

You code is almost right. You just need to define target positions and have Lerp called each time in update function.

A simple solution is to define two empty objects as position targets and pass them as parameters to the function.

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