Need help converting C# to GDScript

Godot Version

<stable4.2>

Question

<Im trying to make arc projectile and found an article for unity with C#. I would appreciate if you could help me to convert this to gdscript.

public class BallController : MonoBehaviour {

    public GameObject block;
    Vector3 offset;
    Vector3 target;
    float deg;
 
    IEnumerator ThrowBall()
    {
        float b = Mathf.Tan (deg * Mathf.Deg2Rad);
        float a = (target.y - b * target.x) / (target.x * target.x);
      
        for (float x = 0; x <= this.target.x; x+= 0.3f)
        {
            float y = a * x * x + b * x;
            transform.position = new Vector3 (x, y, 0) + offset;
            yield return null;
        }
    }

    public void SetTarget(Vector3 target, float deg)
    {
        this.offset = transform.position;
        this.target = target - this.offset;
        this.deg = deg;

        StartCoroutine ("ThrowBall");
    }

    void Start()
    {
        // ブロックに向かって60度の角度で射出
        SetTarget ( block.transform.position, 60 );
    }
}

if nobody converts this to gdscript, here is a video I found on YouTube.

here is an example. gdscript veterans can probably improve. ball (white color rect) throws itself at target (red color rect)

extends ColorRect

@onready var m_target : ColorRect = $"/root/Test/Target"

var m_speed : float = 100
var m_max_height : float = 100

var m_position_y : float
var m_distance : float

func _ready() -> void:
	print ("Hey, look out!")
	
	# just an example so i am going to cheat
	# have target.y be same as ball.y
	m_target.position.y = position.y
	
	
	#distance from ball to target
	m_distance = m_target.position.x - position.x
	
	#original y position of ball
	m_position_y = position.y

func _physics_process(a_delta: float) -> void:
	# move the ball
	position.x += m_speed * a_delta
	
	#distance from ball to target
	var v_distance : float = m_target.position.x - position.x
	
	# how far from ball to target
	#  0.0  to 1.0
	var v_progress : float = 1 - (v_distance / m_distance)
	
	# v_sin is 0.0  to  PI, then make negative
	# just for fun, remove the - and see what happens
	# note sin() returns radians
	var v_sin : float = -sin(v_progress * PI)
	
	# set position
	position.y = m_position_y + (v_sin * m_max_height)
	
	# we reached the target
	if(v_progress > 1):
		print("Ow!")
		
		# Time out before someone gets hurt
		set_physics_process(false)


2 Likes

thank you shrooms. what if you want to take account of gravity ?

for anyone who is looking for a code that works with constant speed and direction.

extends Area2D

@onready var target : Marker2D = $"/root/test/Marker2D"

var max_height : float = 10
var move_time : float = 2.0  # Time in seconds to reach the target

var start_position : Vector2
var distance : float
var target_position : Vector2
var elapsed_time : float = 0.0


func _ready() -> void:

	# Initialize start and target positions
	start_position = global_position
	target_position = target.global_position
	distance = start_position.distance_to(target_position)


func _physics_process(delta: float) -> void:

	# Update the elapsed time
	elapsed_time += delta

	# Calculate the progress (0.0 to 1.0)
	var progress : float = min(elapsed_time / move_time, 1.0)

	# Calculate the new position
	var new_x : float = lerp(start_position.x, target_position.x, progress)
	var linear_y : float = lerp(start_position.y, target_position.y, progress)
	var v_sin : float = -sin(progress * PI)

	# Set the new position
	global_position.x = new_x
	global_position.y = linear_y + (v_sin * max_height)

	# Check if we reached the target
	if progress >= 1.0:
		print("Ow!")
		set_physics_process(false)
		queue_free()
	pass

# Linear interpolation function
func lerp(start: float, end: float, weight: float) -> float:
	return start + weight * (end - start)

I think I managed to make one with gravity.

var gravity: float = 500.0
var time_to_reach: float = 2.0 # プレイヤーに着地するまでの時間
var max_height: float = 100.0 # 弧の高さ

func _init():
	set_as_top_level(true)
	calculate_velocity()

func _physics_process(delta):
	# 重力の影響を受ける
	velocity.y += gravity * delta

	# キャラクターを移動
	move_and_slide()

func calculate_velocity():
	# 現在位置とプレイヤーの位置を取得
	var start_position = global_position
	var target_position = target.global_position

	# X方向の速度を計算
	var distance_x = target_position.x - start_position.x
	var velocity_x = distance_x / time_to_reach

	# Y方向の速度を計算
	var distance_y = target_position.y - start_position.y
	var velocity_y = (distance_y - 0.5 * gravity * time_to_reach * time_to_reach) / time_to_reach

	# 速度を設定
	velocity = Vector2(velocity_x, velocity_y)
1 Like