简体   繁体   中英

Unity 5: How to make an object move to another object?

I want to know how to move an object to another object smoothly, as if I'm throwing it the other object.

Say I have Object A and Object B.

I want, if I click Object B, then Object A will smoothly go to Object B.

I did this:

using UnityEngine;
using System.Collections;

public class GreenEnvelope : MonoBehaviour
{
void Start()
{
    if(Input.GetMouseButton(0))
    {
        Update();
    }
}
void Update()
{
    GreenMail();
}

private void GreenMail()
{
    //the speed, in units per second, we want to move towards the target
    float speed = 40;
    float rotateSpeed = 100f;

    //move towards the center of the world (or where ever you like)
    Vector3 targetPosition = new Vector3(-23.77f, -9.719998f, 0);

    Vector3 currentPosition = this.transform.position;
    //first, check to see if we're close enough to the target

        if (Vector3.Distance(currentPosition, targetPosition) > .1f)
        {
            Vector3 directionOfTravel = targetPosition - currentPosition;
            //now normalize the direction, since we only want the direction information
            directionOfTravel.Normalize();
            //scale the movement on each axis by the directionOfTravel vector components

            this.transform.Translate(
                (directionOfTravel.x * speed * Time.deltaTime),
                (directionOfTravel.y * speed * Time.deltaTime),
                (directionOfTravel.z * speed * Time.deltaTime),
                Space.World);
            transform.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);
         }
     }
}

But I have to keep clicking the object in order for it to move... I have to keep clicking it everyFrame... that's not what I want. I want to click my "Object B" just once, and my "Object A" will just smoothly go to "Object B"

1- Update method is a method that run in every frame . So you need to detect mouse click in update method and then call another method, something like this:

void Start()
{
}

void Update()
{
    if(Input.GetMouseButton(0))
    {
        // Do what ever you want
    }
}

2- This movement must be in Update method to work smoothly, For this, you can use a Boolean flag. Like this:

using UnityEngine;
using System.Collections;

public class GreenEnvelope : MonoBehaviour
{
    bool isMove = false;
    float speed = 40;
    Vector3 targetPosition;
    Vector3 currentPosition;
    Vector3 directionOfTravel ;

    void Start()
    {
    }

    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            isMove = true;
        }

        if (isMove == true)
        {
            GreenMail();
        }
    }

    private void GreenMail()
    {
        targetPosition = objB.transform.position; // Get position of object B
        currentPosition = this.transform.position; // Get position of object A
        directionOfTravel = targetPosition - currentPosition;
        if (Vector3.Distance(currentPosition, targetPosition) > .1f)
        {
            this.transform.Translate(
                (directionOfTravel.x * speed * Time.deltaTime),
                (directionOfTravel.y * speed * Time.deltaTime),
                (directionOfTravel.z * speed * Time.deltaTime),
                Space.World);
         }
         else
         {
             isMove = false;
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM