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! :(
TRY LOOKING INTO VECTOR3.LERP/SLERP OR QUATERNION.LERP/SLERP :P
That will allow you to smoothly move an object from position A to position B. It's tricky to use correctly, but it should help in this case.
Your answer
Follow this Question
Related Questions
Isometric Movent Issue 0 Answers
Character jitters when Moving my camera 0 Answers
Car coding help 0 Answers
how to get flashlight to move with camera 0 Answers
Somersault kick does not work 0 Answers