Godot Version
4.5.1
Question
Hi, I would like to work out what Distance the Golf ball travels, yards is used in Golf.
If you share code, please wrap it inside three backticks or replace the code in the next block:
using Godot;
using System;
public partial class NewBall : RigidBody2D
{
[Export]
private ProgressBar powerProgressBar;
[Export]
private int power = 0;
[Export]
private float dir = 0;
[Export]
private float distance;
[Export]
private RigidBody2D golfBall;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _Input(InputEvent evt)
{
float currentSpeed = .1f;
if (evt.IsActionPressed("ui_left"))
{
dir -= currentSpeed;
}
if (evt.IsActionPressed("ui_right"))
{
dir += currentSpeed;
}
}
private Vector2 GetballDirection()
{
return Vector2.Up.Rotated(dir);
}
public void PlayShot(float power)
{
golfBall.ApplyImpulse(GetballDirection() * power);
CalculateDistance();
}
public Vector2 GetBallPosition()
{
//Get golf ball Position
return golfBall.Position;
}
public float CalculateDistance()
{
//calculate in yards
return distance;
}
private void StartPowerBar()
{
}
private void StopPowerBar()
{
//PlayShot((float)powerProgressBar.Value);
power = (int)(float)powerProgressBar.Value;
PlayShot(power);
}
}