- Home /
move object and slow down near end
I have a spaceship flying in towards a planet (Sphere), rotating and landing. Its working fine, i just want a litle better, im using the Vector3.Slerp and Quaternion.Slerp so that it slows down as it nears the landing spot. But it slows down too mutch, at the end its sooo slow to rotate and land. Any way to make it slow down...slower?
this is the code im using
transform.position = Vector3.Slerp (transform.position, landingSpot, flightSpeed * Time.deltaTime / 100);
if (Vector3.Distance (transform.position, landingSpot) < rotateForLandingDistance) {
transform.localRotation = Quaternion.Slerp (transform.rotation, landingRotation, rotationSpeed * Time.deltaTime / 100);
}
Have you tryied to change the third Lerp param to higher values?
Answer by $$anonymous$$ · May 13, 2015 at 09:45 PM
Time for a slightly more complex answer then! This is by no means the simplest solution but it is probably one of the most flexible.
By making use of animation curves one can draw a curve (in the editor) to manipulate the speed of an object. Below is a simplified version of a script that does that. If you want to actually use this you will probably want to make changes to script and include a base speed to prevent not moving but this will allow you to achieve the variable speed affect you would like.
I'll post the script below but you may want to check out these pages for reference:
The actual script:
using UnityEngine;
using System.Collections;
public class SlerpTowards : MonoBehaviour
{
public Transform Target;
public AnimationCurve DistanceVersusSpeed;
private float _initialDistanceToTarget;
private float _speed;
private float _distanceToTarget;
public void Start()
{
_initialDistanceToTarget = (Target.position - transform.position).magnitude;
_speed = 0;
}
public void Update()
{
// Calculate our distance from target
Vector3 deltaPosition = Target.position - transform.position;
_distanceToTarget = deltaPosition.magnitude;
// Update our speed based on our distance from the target
_speed = DistanceVersusSpeed.Evaluate((_initialDistanceToTarget - _distanceToTarget) / _initialDistanceToTarget);
// If we need to move father than we can in this update, then limit how much we move
if (_distanceToTarget > _speed)
deltaPosition = deltaPosition.normalized * _speed;
// Set our position
transform.position += deltaPosition * _speed * Time.deltaTime;
}
}
This could potentially work for changing any variable you wanted. Animation curves just allow you to "animate" a float. What you choose to do with that float is up to you.
You'll note that in the above example I used the animation curve to animate my _speed variable. I then used this as the rate for which the object moved.
Something along the lines of
transform.rotation += (angle) * _speed * Time.deltaTime
would work for rotation, and more generically:
object.thingToChange += amountToChange * _speed * Time.deltaTime
Your answer
Follow this Question
Related Questions
Slerp look at direction not working... 3 Answers
calculating looking angle between 2 transforms 0 Answers
Distribute terrain in zones 3 Answers
Quaternion Smooth Rotation 0 Answers
Slerp to make the right/left side face another object 2 Answers