My code to charge up a golf shot just drops the ball with no power?

This is my code:
using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour
{
	public Rigidbody projectile;
	public Transform shotPos;
	float shotForce = 1000f;
	public float moveSpeed = 10f;
	
	float Charge(float number){
		float ret;
		ret = number * Time.deltaTime;
		return ret;
	}

	void Update ()
	{
		float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
		float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
		
		transform.Translate(new Vector3(h, v, 0f));
		
		if(Input.GetButtonUp("Fire1"))
		{
			Rigidbody shot = Instantiate(projectile, shotPos.position, shotPos.rotation) as Rigidbody;
			shot.AddForce(shotPos.forward * Charge(shotForce));
		}
	}
}

It just drops and I can’t work out why, I’m new so let me know if I missed anything out. Thanks in advance.

Your Charge function only gets run during one frame, when the user releases the mouse button. Time.deltaTime is the amount of seconds that have passed since the last frame, so for a 30 frames-per-second game, that’ll be about .033 ms, every time. So you can see that 1000 * .033 is a little less than you were probably hoping for.

What you need instead of Time.deltaTime (the time between the last frame and the current frame), is the time delta between when the user first pressed the Fire1 button down, and when they released it. You’ll need a new member variable for that. Try something like this:

float chargeTimeBegin; // add this with your existing other variables

// Charge now requires 2 parameters, 1 for the power, 1 for the chargedTime
float Charge(float number, float chargedTime){
	return number * chargedTime;
}
	
void Update () {
	// your transform-handling code here...

	// charging code...
	if (Input.GetButtonDown("Fire1"))
	{
		// mark this as the time we started charging
		chargeTimeBegin = Time.timeSinceLevelLoad;
	}
	else if (Input.GetButtonUp("Fire1"))
	{
		// measure how many seconds the mouse button was held down for.
		float chargedTime = Time.timeSinceLevelLoad - chargeTimeBegin;
		Rigidbody shot = Instantiate(projectile, shotPos.position, shotPos.rotation) as Rigidbody;
		shot.AddForce(shotPos.forward * Charge(shotForce, chargedTime));
	}
}

Thank you for the information!

Facebook PVA

Hey, you’ll want to add a timer that determines how long the button is held for in FixedUpdate or Update, as it needs to increase time every frame as opposed to once. So something like this might work:

public class Shooter : 
        MonoBehaviour
        {
            public Rigidbody projectile;
            public Transform shotPos;
            float shotForce = 1000f;
            public float moveSpeed = 10f;
            public float charge;
        
            public bool charging=false;
           
         
            void Update ()
            {
                float h = Input.GetAxis("Horizontal") *Time.deltaTime * moveSpeed;
                float v = Input.GetAxis("Vertical") * Time.deltaTime *moveSpeed;
         
                transform.Translate(new Vector3(h, v, 0f));
        
                if(charging)
                {
                    charge=shotForce*Time.deltaTime;
                }
        
                if(Input.GetButtonDown("Fire1"))
                {
                    charging=true;
                }

                if(Input.GetButtonUp("Fire1"))
                {
                    charging = false;
                    Rigidbody shot = Instantiate(projectile, shotPos.position, shotPos.rotation) as Rigidbody;
                    shot.AddForce(shotPos.forward * charge);
                    charge=0;
                }
            }
        }

Hope it helps :slight_smile: