Hi. I try to put together a raycast bullet script so that i can use visible bullets by shooting and particles at once.
But it works terible :neutral: . The ray cast is ok but the bullets are shooting backwards and only if i turn left or right.
Im only an amateur. Can someone help? 1191283–46816–$GunScript.js (1.66 KB)
var Range : float = 250;
var Force : float = 200;
var Clips = 20;
var BulletsPerClip : int = 60;
var BulletsLeft : int = 0;
var UntaggedParticle : ParticleEmitter;
var hitparticles : ParticleEmitter;
var Bullet : GameObject;
var Spawn : GameObject;
var BulletSpeed : int = 700;
var Ammo : int = 30;
var FireModes : String = “Semi”;
function Start () {
BulletsLeft = BulletsPerClip;
}
function Update() {
AndersUpdate() ;
}
function AndersUpdate() {
if(Input.GetButton(“Fire3”)&BulletsLeft){
ShootFire();
}
if(FireModes == “Semi”)
{
if(Input.GetButton(“Fire3”))
{
FireOneBullet () ;
}
}
}
function FireOneBullet ()
{
var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation);
Bullet.rigidbody.AddForce(transform.forward * BulletSpeed);
Ammo-- ;
}
function ShootFire(){
var direction : Vector3 = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var position = transform.parent.position;
var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var Bullet : GameObject;
Debug.DrawRay(transform.position, direction * Range, Color.blue);
if(Physics.Raycast(transform.position, direction, hit, Range));
if(hit.rigidbody) {
hit.rigidbody.AddForceAtPosition(Force * direction, hit.point);
hitparticles.transform.position = hit.point;
hitparticles.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitparticles.Emit();
}
if(hit.transform.tag == “Untagged”){
UntaggedParticle.transform.position = hit.point;
UntaggedParticle.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
UntaggedParticle.Emit();
}
}