- Home /
Make tranform follow raycast
Hello, I've been trying to make a weapon script which fires a projectile (prefab) straight forward.I added to the bullet the first script shown here.And the second one to the main camera.The problem is that the gun doesn't shoot straight forward.I mean it shoots straight but in small distances it looks...crap.How can I make it so it follows the hit point of the raycast.I placed my attempt below.
Script 1:
var bulletSpeed : float = 20;
function Update ()
{
transform.Translate(Vector3.forward * bulletSpeed * Time.deltaTime);
}
Script 2 :
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, forward, hit, rayLength))
{
if(Input.GetButtonDown("Fire1") && canShoot==true && clip > 0)
{
var rocketInstance : Transform;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, transform.rotation);
rocketPrefab.LookAt(hit.point);
w();
gun.animation.Play("Shoot");
audio.PlayOneShot(soundEffect);
clip -= 1;
}
}
Thank you for your time,please help me!
Answer by robertbu · Jul 24, 2014 at 10:05 PM
Your problem is that you are using LookAt() on the wrong object. You want to do the LookAt on the rocketInstance.
rocketInstance.LookAt(hit.point);
Your answer
Follow this Question
Related Questions
Raycast script help? 1 Answer
Switch GameObjects Tags with javascript 1 Answer
function OnCollosionEnter problems 1 Answer
Creating a teleportation gun 1 Answer
Combo Script Problem 1 Answer