- Home /
Rotation and Moving object
Hi, I dont want to spam all the time with questions, so here is 3 questions: 1. I found this script for moving some object when trigger get on, but i want and that the same object move back, in starting position, if someone know how to do it please help:
public Transform sign;
public Vector3 signTargetOffset;
public float raiseTime = 2;
Vector3 signStart;
Vector3 signTarget;
public GameObject player;
void Start()
{
signStart = sign.position;
signTarget = signStart + signTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject == player)
{
StartCoroutine(RaiseSign(raiseTime));
}
}
IEnumerator RaiseSign(float time)
{
float elapsedTime = 0;
while(elapsedTime < time)
{
sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
sign.position = signTarget;
}
need as well that some other object rotate in Z axis for some time, and then stop for lets say 2 sec and then again rotate, also if someone know this pls help 3.And for the last one if u can help me with making script for some object to move in Z axis and then somewhere stop and go back in starting position, and do that all the time.
Thanks everybody who answer on any of this question!
Unless the questions are very relevant to each other feel free to create separate questions im the future. It might seem that you're asking a lot of questions at the same time, but in the long run people will be able to find back and google the question-answer relationships much easier if everything is focused on a single simple thing. Currently I'll see if I can answer this one for the biggest part here. :)
Answer by BigRoy · Jan 02, 2014 at 03:24 PM
1. Move to position on OnTriggerEnter() and move back on OnTriggerExit()
The following should start the raiseSign coroutine whenever the player enters the trigger. It force stops that coroutine whenever it leaves it again and then starts the lowerSign coroutine (and vice versa).
public Transform sign;
public Vector3 signTargetOffset;
public float raiseTime = 2;
public GameObject player;
private Vector3 signStart;
private Vector3 signTarget;
private bool isEnabled = false;
private float elapsedTime = 0;
void Start()
{
signStart = sign.position;
signTarget = signStart + signTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if(!isEnabled && other.gameObject == player)
{
isEnabled = true;
StopCoroutine("LowerSign");
StartCoroutine("RaiseSign", raiseTime);
}
}
void OnTriggerExit(Collider other)
{
if(isEnabled && other.gameObject == player)
{
isEnabled = false;
StopCoroutine("RaiseSign");
StartCoroutine("LowerSign", raiseTime);
}
}
IEnumerator RaiseSign(float time)
{
// Move to the raised position
while(elapsedTime < time)
{
sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
elapsedTime = time;
sign.position = signTarget;
}
IEnumerator LowerSign(float time)
{
// Come back from the raised position
while(elapsedTime > 0)
{
sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
elapsedTime -= Time.deltaTime;
yield return null;
}
elapsedTime = 0;
sign.position = signStart;
}
Old (Before edit:) I had this here before. I misunderstood the question, but I'm keeping it here as a reference on how to do the 'back and forth' motion at once.
public Transform sign;
public Vector3 signTargetOffset;
public float raiseTime = 2;
Vector3 signStart;
Vector3 signTarget;
public GameObject player;
void Start()
{
signStart = sign.position;
signTarget = signStart + signTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject == player)
{
StartCoroutine(RaiseSign(raiseTime));
}
}
IEnumerator RaiseSign(float time)
{
// Move to the raised position
float elapsedTime = 0;
while(elapsedTime < time)
{
sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
sign.position = signTarget;
// Move back from the raised position
elapsedTime = 0;
while(elapsedTime < time)
{
sign.position = Vector3.Lerp(signTarget, signStart, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
sign.position = signStart;
}
Note about your collisions and the Coroutine running multiple routines at the same time.
Note that since you're using a Coroutine for this that it is possible that the player runs into the collision area, runs out in the meantime and back in while it's still performing the routine. This will result in having two of the same Coroutines going on at being performed at the same time.
A possible solution is to have a variable that stores whether it's already raising the sign, if so it will not trigger it again until it ended. (Shouldn't be too hard to add. Let me know if you want me to add an example of this quickly.)
2. Move more objects
This should be a slight modification on the script above. You can add extra public Transform sign variables and then also include those in the coroutine, or you could add extra coroutines for them.
To apply rotational changes instead of doing sign.position use sign.rotation. You can do this because the sign variable in the script is actually a Transform object.
3. Keep moving an object ever since the first collision.
This sounds like it's activating another script. Instead of writing out the script for you I'll give you some pointers, here are some possible ways to proceed:
Enable a component on the object you want to keep moving from within this script
Set a public variable on another object to True and perform your movements in that script.
You could also (I think) initialize a looping animation from that point using Unity's animation system.
I dont think u understood me for this 1st question ( or more like i didnt explain good) I dont want to it go back right when it come to its destination, I want to it stay there while Player dont Exit collision area, I will look other 2 questions later, I dont have time now, but thanks anyway if u dont know how to do this what i want
@misko96 I did misunderstand the question, sorry about that. I edited my answer based on your comment. At the top you should find code that should do exactly what you described.
For this 2nd question again my english is sooo bad so problably i didnt explain it good either, it doesnt need to be connected to any collider or whatever, i just want that it rotate independently on anyhing, just that it rotate for lets say 3 sec, then stops, and then again rotate, and same for 3rd question, just that it move, and come back and again and again( even i didnt understand your answer on that question either, so maybe u wanted to say that)
Your answer
![](https://koobas.hobune.stream/wayback/20220613125342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotate overtime to match waypoint 1 Answer
Change speed over time 1 Answer
Rotating an Object while Moving 0 Answers