I am trying to make a ball fly to a targeted position and make it look like it has been hit. I currently have this code:
transform.position = Vector3.Lerp(transform.position, new Vector3(9f, 13.56091f, 0f), Time.deltaTime * 10);
this makes the ball move to its new position but it does not make it look like the ball has been hit, flies into the air and arrives at the new position.
Can anyone help me make this look realistic please?
I’m in a good mood so I’ll pass this on … may need tiding up.
var ballToThrow : GameObject;
var player : Transform;
private var ballCounter : boolean;
function Update(){
throwBall();
}
function throwBall(){
ballCounter = false; // Rest to false so another ball can be thrown
ballThrow();
}
function ballThrow(){
if(!ballCounter){ // Set so you will only throw 1 ball at a time
var ball: GameObject = Instantiate(ballToThrow, GameObject.Find("BallThrowPoint").transform.position, transform.rotation);
ball.rigidbody.velocity = BallisticVel(player);
ballCounter = true;
}
}
// Work out the velocity and angle to the player for the Balls trajectory
function BallisticVel(target: Transform): Vector3 {
var dir = target.position - transform.position; // Get target direction
var h = dir.y; // Get height difference
dir.y = 0; // Retain only the horizontal direction
var dist = dir.magnitude ; // Get horizontal distance
dir.y = dist; // Set elevation to 45 degrees
dist += h; // Correct for different heights
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude);
return vel * dir.normalized; // Returns Vector3 velocity
}