I want this code to work when i press a key, not on its own:

void Start(){

	shootAt (target);
			
					
			}
	

void shootAt(Transform newTarget){
			//if (Input.GetKeyDown ("space")) {
					target = newTarget;
	
					time = timeTravel; // in seconds
	
					origin = transform.position;
					destination = target.position;
			
	}

It basically zooms towards a target object as soon as i hit play. I dont want that. I know i need to use the Update() function to seet the input button for this one but I am lost. I want the projectile to hit target only on button press. Thanks…

void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
shootAt(target);
}
}

If you’re going to do anything physics related (such as moving objects in your game), you’ll want to use a combination of Update() and FixedUpdate(). If your shootAt() function will take care of actually firing a projectile in the game, you’ll want to call it in FixedUpdate(). The reason is because Update() is called every frame, and you don’t want a player to be shooting faster or slower just because they have a higher/slower frame rate.

So a solution could be:

bool shooting = false;

void Update() {
    if( Input.GetKeyDown(KeyCode.Space) )
        shooting = true;
}

void FixedUpdate() {
    if ( shooting ) 
        shootAt( target );
}