Hey guys,
I’m currently writing a script in which a projectile is shot each time an asteroid is tapped. My only problem is that I want the projectile to shoot toward the asteroid by looking at it. I’ve tried to make the origin of the projectile (named Shooter) look at the asteroid but haven’t had any luck. I’m unsure of where to go from here and could really use a nudge in the right direction. My code is below, thank you for your help.
//js
var Shooter : Transform;
var Asteroid : Transform;
var projectile : Rigidbody;
function Update()
{
//check for touches
if (Input.touchCount > 0)
{
//grab touch
var t = Input.touches[0];
//check if it's the beginning of a touch
if (t.phase == TouchPhase.Began)
{
//shoot a ray from the touch position into the scene
var ray = Camera.main.ScreenPointToRay(t.position);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) //check if the ray is hitting something
{
var clone : Rigidbody;
Shooter.LookAt(Asteroid);
clone = Instantiate(projectile, transform.position, Asteroid.rotation);
clone.velocity = transform.TransformDirection (Vector3.up * 45);
}
}
}
}