- Home /
Making projectiles move to a tapped object- Mobile
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);
}
}
}
}
Answer by BalsamicVinegar · Feb 01, 2012 at 07:04 AM
You aren't setting the asteroid to anything. I'm guessing you need to do this:
Asteroid = hit.collider.gameObject;
Then tell Shooter to look at the asteroid.
Also; pre-empting another possible problem; I'm pretty sure you want to use your transform.forward in the velocity set. E.g. clone.velocity = transform.forward * 45;
Your answer
Follow this Question
Related Questions
Mobile platformer jump not working 2D 1 Answer
[iOS/Android] U3D repeats tap to new scene 0 Answers
Script when tapping an object 2 Answers
Mobile Tap Issue 1 Answer
How to make mouse click and tap work in same cases on boxCollider2d 1 Answer