Thanks for checking in.
This request is by all accounts too early to be made, but my wife needs me to go run some errands with her the rest of the night and I haven’t done enough research to solve this yet. So I figured I might as well throw this out if someone had the minute to point me in the right direction.
(I am still learning how to script and how to think like a programmer so this most likely will be awfully simple…)
Basic goal is to Instantiate a ball that moves in a straight line from a start point to an end point. This carries indefinitely until triggered to stop.
I have considered this as a solution:
// Animates the position to move from start to end within one second
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
Using Vector3.Lerp here I end up with my ball instantiating on trigger and moving from start to end in one second. Not a slow movement that looks like a ball bounding back and forth off two walls.
Using the code below gets me what I want, however depending on the timing of hitting the trigger I get an instantiated ball in a different moment of the animation from starting point to end point. So first second it is maybe three clicks back from middle, but if I wait two seconds it is almost at the end point wall. This is because the movement code is running in a fixed update that is constantly running in the background whether or not i have instantiated the gameobject that is moving.
private var start : GameObject;
private var end : GameObject;
var speed : float = 0.2;
function Start(){
start = GameObject.Find("xStart");
end = GameObject.Find("xEnd");
}
function FixedUpdate () {
var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5;
transform.position = start.transform.position * weight
+ end.transform.position * (1-weight);
}
So, the question is:
What can I use to have the ball have a permanent movement on it going back and forth THAT STARTS ONLY from the point of instantiation?
I already made the mistake of putting the movement script into the IF statement of my script that also instantiates the balls, but since the movement was coded to perform only on trigger (of course) it only would move when I held down the trigger.
So, my choice of a fixed update is great for the 2dSidescroller hovering platform lerpz can ride…but not for a ball I want to make ROUGHLY the same generic repetitive movement but START its movement on trigger.
I know this might not make sense or seem too simple, so let me emphasize that this is not important. Unless you got the time, please ignore this request. I will eventually get back from helping the wife and will spend tomorrow or late tonight figuring this out.
Thanks!