- Home /
moving platforms?
so i was wondering how to make a back and forth moving platform on which my player sticks without sliding off. so far i have this script:`public class platform1 : MonoBehaviour {
public float moveSpeed;
bool end1;
bool end2;
float origin;
private void Start()
{
System.Random rnd = new System.Random();
moveSpeed = rnd.Next(1, 10);
origin = gameObject.transform.position.z;
end1 = true;
}
void Update ()
{
if (gameObject.transform.position.z < origin -5)
{
end1 = true;
}
if (end1 == true)
{
transform.Translate(moveSpeed * Vector3.forward * Time.deltaTime);
end2 = false;
}
if (gameObject.transform.position.z > origin +5)
{
end2 = true;
}
if(end2 == true)
{
transform.Translate(moveSpeed * -Vector3.forward * Time.deltaTime);
end1 = false;
}
}
}`
so i want to, without completely overhauling what i have, make it so this script (possibly?) gets information about the players distance relative to the position of the platform object and adds that offset to the player's transform. If what i just said even makes sense.
also, ignore the RNG thing that i tried with the whole use of System.Random. I was attempting to make it so that the platforms using this script would have randomly set speeds. I think this worked? but it made all the platforms have the same speed as each other even though it was randomly generated. Perhaps this is an endeavor i'll figure out later (Or Extra Credit to those who can help me figure it out!). I'm mostly just focusing on just making the platform behave the way i want it to with the player.
I'm very very new to scripting so any help at all would be awesome! :)
Your answer
Follow this Question
Related Questions
How to change the forward direction of a player and make turning smoother 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving Platform Help (c#) 3 Answers
Player movement on tilting platform 0 Answers