- Home /
Question by
LT23Live · Jul 29, 2014 at 01:19 AM ·
transformvector3ontriggerentervector3.lerptranform.position
How to make object move smoothly.
I have a script to make rocks float when I step onto a specific Collider.
The script i have works but it just changes the position in a flash. I want the rocks to flow smoothly :/
Here is the script.
using UnityEngine;
using System.Collections;
public class Rock_Levitate : MonoBehaviour {
public GameObject floatingRock;
public GameObject collider;
public GameObject startPosition;
public GameObject endPosition;
public float moveSpeed = 10.0f;
// Use this for initialization
void Start () {
floatingRock.transform.position = new Vector3 (startPosition.transform.position.x, startPosition.transform.position.y, startPosition.transform.position.z);
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider col){
if (col.gameObject.tag == "Player"){
print ("Player has touched Rock Float Trigger");
float step = moveSpeed * Time.deltaTime;
floatingRock.transform.position = Vector3.Lerp (startPosition.transform.position, endPosition.transform.position, moveSpeed);
print ("Rock is Moving.");
}
}
}
Comment
OnTriggerEnter
is only called once, when you enter the trigger. You need to call Lerp
every frame to do a smooth interpolation.
Answer by smallbit · Jul 29, 2014 at 01:50 AM
Maybe
floatingRock.transform.position = Vector3.Lerp (startPosition.transform.position, endPosition.transform.position, step);
Instead of
floatingRock.transform.position = Vector3.Lerp (startPosition.transform.position, endPosition.transform.position, moveSpeed);
You calculate "step" correctly by multiplying your speed parameter by time.deltatime, but yet u use "speed" in lerp.
Answer by Thanitsak · Apr 28, 2016 at 05:12 AM
Perhaps, you need to use rigidbody.addforce. Its would help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155238im_/https://answers.unity.com/themes/thub/images/avi.jpg)