- Home /
How to reset game object to original position after isFalling = True, C#
Hello all,
I'm new to scripting and figuring this out as I go for a team game build for school. The game we're making is a 3D platformer where the object is to run across platforms, reach the end while the platforms fall as you run.
At the moment I have the platforms setup to fall from an OnTriggerEnter collision with the Player object. The collision sets the platform to isFalling "true" and makes it fall down at a set speed. What I can't figure out is how to force the platform object to stop falling and reset back to it's original local position after reaching a certain point on the Y axis? The public int maxYPosition should tell the object what it's max Y position will be before it resets to the starting position.
The last if statement in the Void Update area is where I attempted to accomplish this but it has just made it so the platforms stay in place now. I've pieced the script below together from various tutorials/answers I've found but my limited scripting knowledge has made it tough to figure out how to make the platform do exactly what I would like it to do. Any help here would be much appreciated.
Thanks
public class ps_Falling_Platform : MonoBehaviour {
public int maxYPosition = -100;
bool isFalling = false;
float downSpeed = 0;
private Vector3 initialPos;
private Quaternion initialRotation;
void Start()
{
initialPos = transform.localPosition;
initialRotation = transform.localRotation;
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.name == "Player")
{
isFalling = true;
}
}
void Update()
{
if (isFalling)
{
downSpeed += Time.deltaTime/150;
transform.localPosition = new Vector3(transform.localPosition.x,
transform.localPosition.y-downSpeed,
transform.localPosition.z);
}
if (gameObject.transform.localPosition.y >= maxYPosition)
{
transform.localRotation = initialRotation;
transform.localPosition = initialPos;
isFalling = false;
}
}
}