The question is answered, right answer was accepted
Moving GameObject slowly OnTriggerEnter
Hello, I’m new in this forum and also in Unity,
and I hope that one of you will help me. I Created a script that I use when an object enters in specific zone (collider), an other object move from its position to another (it depends on the axis) following a certain distance.
The script works well, but I want that the movement occurs slowly. I tried many things that I found in internet but nothing works.
Hope you can help me.
This is my script :
using UnityEngine; using System.Collections;
public class MoveUpObject : MonoBehaviour {
private Vector3 startPos;
public float distance = 1.0f;
public float speed = 1.0f;
public enum MoveAxe {AxeX, AxeY, AxeZ};
public MoveAxe moveAxe;
public GameObject targetGameObject = null;
void Start () {
startPos = targetGameObject.transform.position;
}
void OnTriggerEnter (Collider other) {
Vector3 endPos = startPos;
if (moveAxe == MoveAxe.AxeX) {
endPos.x += distance;
} else if (moveAxe == MoveAxe.AxeZ) {
endPos.z += distance;
} else if (moveAxe == MoveAxe.AxeY) {
endPos.y += distance;
}
float step = speed * Time.deltaTime;
targetGameObject.transform.position = Vector3.Lerp (endPos, startPos, step);
}
void OnTriggerExit (Collider other) {
targetGameObject.transform.position = startPos ;
}
void Update () {
}}
Answer by AZIIZoom · Jan 18, 2018 at 09:49 PM
I Finally found a solution, I used another approach,
In the Start () function, I initialized the start position StartPos and the end position EndPos. On the OnTriggerEnter () and OnTriggerExit functions, I just initialized the displacement computation time and added a bool variable that indicates if there is an A object or not in the detection zone. Then, I put the formula that moves the object into the Update () function; and as the case may be: -if object A enters the detection zone, object B moves from its StartPos position to the EndPos position -if object A leaves the detection zone, object B moves from its EndPos position to the StartPos position
public class MoveObject : MonoBehaviour {
private Vector3 startPos;
private Vector3 endPos;
public float distance = 1.0f;
public float lerpTime = 1.0f;
private float currentLerpTime = 0;
public enum MoveAxe {AxeX, AxeY, AxeZ};
public MoveAxe moveAxe;
public GameObject targetGameObject = null;
private bool objectIn;
void Start () {
startPos = targetGameObject.transform.position;
endPos = targetGameObject.transform.position;
if (moveAxe == MoveAxe.AxeX) {
endPos.x += distance;
} else if (moveAxe == MoveAxe.AxeZ) {
endPos.z += distance;
} else if (moveAxe == MoveAxe.AxeY) {
endPos.y += distance;
}
}
void OnTriggerEnter (Collider other) {
objectIn = true;
currentLerpTime = 0;
}
void OnTriggerExit (Collider other) {
objectIn = false;
currentLerpTime = 0;
}
void Update () {
if (objectIn == true) {
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerpTime) {
currentLerpTime = lerpTime;
}
float perc = currentLerpTime/lerpTime;
targetGameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
} else
if (objectIn == false) {
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerpTime) {
currentLerpTime = lerpTime;
}
float perc = currentLerpTime/lerpTime;
targetGameObject.transform.position = Vector3.Lerp (endPos, startPos, perc);
}
}
}
Follow this Question
Related Questions
Gameobject doesnt move after build 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers
how to test if something enters a specific trigger and set that object to a gameobject variable!?!? 1 Answer
OnTriggerEnter 2 Answers
Two colliders that trigger different things in the same object-hierarchy 0 Answers