So I got this to work kinda but the idea is to create a initial position for the ball that resets to the new current position if the script on the ball that allows players to put input to the ball is enabled. If its not enabled the Initial Position doesn’t update.
public class Ball_boundry : MonoBehaviour
{
public Vector3 initialPos;
public Quaternion originalRotation;
public Rigidbody rb;
public GameObject GolfBall;
public BallMovement MovementController;
// Use this for initialization
void Start() {
initialPos = GolfBall.transform.position;
originalRotation = GolfBall.transform.rotation;
}
void FixedUpdate () {
if (MovementController.enabled == true) {
initialPos = GolfBall.transform.position;
Debug.Log(“Initial position is happening”);
}
}
void OnTriggerEnter(Collider collide){
if (collide.gameObject == GolfBall)
{
Debug.Log(“collied”);
Reset();
}
}
private IEnumerator RestartBall() {
yield return new WaitForSeconds(5);
Debug.Log(“in restart bal function”);
rb.transform.position = initialPos;
rb.transform.rotation = originalRotation;
}
public void Reset()
{
StartCoroutine(RestartBall());
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
Debug.Log(“function called”);
}
This is controlled by another script that looks at the balls velocity as a vector3 and if it is >0 then that component is set to false and if it is =<0 then it is set to true and allows players to hit the ball again.
public class LevelState_Controller : MonoBehaviour {
// Use this for initialization
// This script is used to control order of functions
public BallMovement Movementcontroller;
public Rigidbody rb;
void Start()
{
rb = GetComponent();
Movementcontroller = GetComponent();
}
// Update is called once per frame
void FixedUpdate() {
if (rb.velocity.x > 0)
{
Movementcontroller.enabled = false;
}
else if (rb.velocity.y > 0)
{
Movementcontroller.enabled = false;
}
else if (rb.velocity.z > 0)
{
Movementcontroller.enabled = false;
}
else
Movementcontroller.enabled = true;
}
}
So this works to a degree. It resets the initial position based on if the script is enabled or not and it only allows players input if the script is enabled or not and that is all good. My issue is when looks to enable or disable the script some times it works great but if the ball moves very slowly it can turn on and off quickly resetting the initial position several times or if it hits a wall it can turn on and then off as it changes direction and resets int pos of the ball. I am kinda getting out of my depth at this point. Not sure if I could add a yield to to resetting the initial position since the player input doesn’t matter. then I could base a score off each time the initial position changes or the reset runs to add one to the stroke. Any ideas or suggests?