- Home /
Shoot in a straight line
Hi,
I am trying to make a car shooter, where you shoot enemy cars. I have this script for a bullet:
//Attach to empty object, in front of barrel
var projectile : Rigidbody;
var speed = 20;
var RotateX = 0;
var RotateY = 0;
var RotateZ = 0;
function Update()
{
if( Input.GetButton( "Fire1" ) ) {
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
// Physics.IgnoreCollision( instantiatedProjectile. collider,transform.root.collider );
}
}
It works fine when I am moving forward, but when I move left to right, the bullets go all over the place. How can I get them to always move in a straight line?
Have you tried the raycast approach? I thinks it's a bit more reliable since there are no actual gameobjects that need to be moved. For visuals you could then use line renderers or something like that..
Answer by BobRon · Mar 17, 2012 at 12:33 AM
Try this doc. Jump down to Adding a Weapon. Creating a First Person Shooter (FPS) - Unity
With this, the bullets still go everywhere. I found a raycast script, but it does not visually display the bullet. How would you visually display the bullet using raycasts?
Easy..
Debug.DrawLine(startRay.position, raycasthit.point)
the startRay variable being the object you spawn the ray from the raycasthit.point is the point where the raycast hit something raycasthit is a variable.
This shows the ray in scene view if you want to see the ray in game enable gizmos
Sorry, but I am new. Here is what I have so far, but it returns NullReferenceException on line 16 and line 7:
var shotSound: AudioClip;
var bloodPrefab: GameObject;
var sparksPrefab: GameObject;
var raystart: GameObject;
function Update(){
if (Input.GetButtonDown("Fire1")){
Shoot();
}
}
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound);
var hit: RaycastHit;
Debug.DrawLine(raystart.position, hit.point);
if (Physics.Raycast(transform.position, transform.forward, hit)){
// prepare rotation to instantiate blood or sparks
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){ // if enemy hit...
if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
hit.transform.Send$$anonymous$$essage("ApplyDamage", 5, Send$$anonymous$$essageOptions.DontRequireReceiver); // and consume its health
} else { // otherwise emit sparks at the hit point
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
}
}
Ok, is it possible to constrain the position on the local axis?
Answer by Dreamside · Mar 17, 2012 at 01:12 PM
You should give velocity to projectile according to forward vector of the car.You always give velocity in z axis. Also you should check if any force being applied on projectile or not.
Your answer
Follow this Question
Related Questions
Freeze Position problem 1 Answer
Wheelcolliders launch the car. 0 Answers
Rigidbody makes NavmeshAgent stutter in its movement 2 Answers
OnTriggerEnter not functioning 0 Answers
Bullet Sometimes Go Throw Object 0 Answers