0

Edited: I've got a slider from 0-90 for the user to select an angle, and a text field for them to enter a speed. All vids I've seen either have people using their own physics or are retrieving an angle directly from the angle of the "gun" or the mouse position, but that's not what I need. I've got it to where gravity affects it now, but it just drops after it spawns, even if I set the speed to 50. I want the speed to determine the dx and dy values at the time the ball is spawned, but it seems that's not happening.

I believe I can get the slider value with GameObject.Find("AngleSlider").GetComponent().angleSlider.value and should be able to do something similar with the text box value for the speed if I can cast it to a float(?) so I think I'm fine there unless anyone notices a problem with that.

Any help is appreciated for this beginner. Thanks!

Edit: By request I've added the code I have atm for this part. This is the ball spawner that I've attached to the cannon. The CannonBall mentioned is another script attached to the cannonball prefab. I have public uninitialized dx, dy, and speed floats in the CannonBall script.

public GameObject cannonBallPrefab;
public Transform shotPoint;

void Start () {

}

void Update () {

    if(Input.GetKeyDown("space"))
    {
        GameObject ball = Instantiate(cannonBallPrefab, shotPoint.position, Quaternion.identity);


        CannonBall ballMovement = ball.GetComponent<CannonBall>();
        ballMovement.dy = Mathf.Sin(GameObject.Find("AngleSlider").GetComponent<SliderHandler>().angleSlider.value * Mathf.Deg2Rad) * ballMovement.speed;
        ballMovement.dx = Mathf.Cos(GameObject.Find("AngleSlider").GetComponent<SliderHandler>().angleSlider.value * Mathf.Deg2Rad) * ballMovement.speed;
        Debug.Log("ball is spawned");
    }
}

0 Answers0