Ok so I am trying to apply a random up / down and force to a rigid body, and after the force is applied after x seconds it reset back to the middle. This is the code I wrote but the rigid body does not stay in the screen max height IE; if up or down force gets applied in a row it will go off camera. I hope I'm explaining this enough. Below are my 1st attempt and I am curious how far off am I? should I just give up on the game dev dream lol. the picture is for visual reference only not actual graphics
public class upDownMovement : MonoBehaviour {
public Rigidbody devilRB;
private float resetPos;
void Start ()
{
StartCoroutine(TimedMovement());
}
private IEnumerator TimedMovement()
{
yield return new WaitForSeconds(3);
float randomHeight = Random.Range(-Screen.height, Screen.height);
if (randomHeight < 1)
{
Debug.Log("Going Down");
resetPos = (Mathf.Sign(randomHeight));
}
if (randomHeight > 0)
{
Debug.Log("Going Up");
resetPos = (Mathf.Sign(-randomHeight));
}
Debug.Log("force to applie: "+randomHeight);
Debug.Log("reverse force: "+resetPos);
devilRB.AddForce(000, randomHeight, 000);
StartCoroutine(TimedReset());
}
private IEnumerator TimedReset()
{
yield return new WaitForSeconds(3);
devilRB.AddForce(000, resetPos, 000);
StartCoroutine(TimedMovement());
}
}
I really didn't think I would run into so many coding issues as I have PHP / action script under my belt ?