HOW TO MOVE A OBJECT TO ANOTHER OBJECT SMOOTHLY NOT INSTANTLY?
HI GUYS! ok I want to know how to move an object to another object smoothly, as if im 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 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... thats not what I want. I want to click my "Object B" just once, and my "Object A" will just smoothly go to "Object B"
Please Help! :(
Your answer
Follow this Question
Related Questions
How to get only one object to select at a time (in game)? 1 Answer
drag AND drop push and pull object 0 Answers
Return key triggering a button OnClick? 1 Answer
Click Button and Follow Mouse 1 Answer
Saving old Position 1 Answer