- Home /
Making The Hole In A Golf Game
I am making a mini golf game. The player clicks on the screen and drags the mouse in the opposite direction of the direction where you want to hit. I am wanting to make the game to be all in one scene, so I want the player to change position when getting the ball in the hole. The player will move to the next hole after he gets the ball in the hole. I can't figure out how to change the position of the player when he gets it in the hole.
This is the player script:
public class Player: MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
private LineTrajectory tl;
public float maxSpeedForGoal;
public Vector2 minPower;
public Vector2 maxPower;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
public bool inHole = false;
Camera cam;
private void Start()
{
cam = Camera.main;
tl = GetComponent<LineTrajectory>();
}
private void Update()
{
if (this.rb.velocity.magnitude <= 0.1f && inHole == false)
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
if (Input.GetMouseButtonUp(0))
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
tl.Endline();
}
}
}
private void OnTriggerEnter2D(Collider2D obj)
{
if (obj.transform.tag == "Hole" && this.GetComponent<Rigidbody2D>().velocity.magnitude < maxSpeedForGoal)
{
inHole = true;
// What do I put here?
}
}
}
Any help would be appreciated. I am a beginner so please make it easy for me. Thank you
Answer by CobbledGames · Mar 29, 2020 at 01:33 PM
What I think could help is if you had transforms on the game map where the player is supposed to start on each hole. Additionally I think you should have an integer for what hole the player is currently on.
When the player enters the hole, the hole number should increase by 1 and then be checked and based on what hole number it is the player's transform.position and transform.rotation should be set to the corresponding hole start transform.