Weapon simulation using raycast?

Post Reply
gtafan
Posts: 14
Joined: Tue Jul 16, 2013 2:57 pm

Weapon simulation using raycast?

Post by gtafan »

Is there some exisitng solution for using weapons in bullet? I found some solution for unity3D that using raycast, but unity3D usese some realy sily programing language here is the sorce:

Code: Select all



var GunCamera : Camera;

var shotSound: AudioClip; // Shot sound

var bloodPrefab: GameObject; // Blood for enemy

var sparksPrefab: GameObject; // Sparks for groun

var muzzleFlash : GameObject;

var ShellPrefab : Transform;

var shootEnabled: boolean = true; // allows disabling the shots

var ChangeWep : boolean = true;

var MaxBullets = 9;

var Bullets = 9;

var Clip = 9000;

var shotInterval = 1.0;

var force = 50;

var RayCastDist : float = 50;

var Zoom : int;

 

function Shoot(){

    var cam : Transform = Camera.main.transform;

    var ray = new Ray(cam.position, cam.forward);

    var hit : RaycastHit;

    var trf = transform; // a little optimization

    var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

    MuzzleFlashOn();

    audio.PlayOneShot(shotSound); // play the shot sound...

    var ShellClone = GameObject.Instantiate(ShellPrefab, GameObject.Find("ShellSpawner").transform.position, Quaternion.identity);

    ShellClone.rigidbody.AddForce(transform.up + transform.right * 200);

 

        if(Physics.Raycast (ray, hit, RayCastDist)){

        var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);

 

            if(hit.collider.gameObject.tag == "Box"){

                Debug.Log ("Hit A Box!");

                if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); 

                hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);

                hit.collider.SendMessageUpwards("ApplyDamage", 5.0);

                //Instantiate(bulletHole, hit.point, rot);

        }

            if(hit.collider.gameObject.tag == "Enemy"){

                Debug.Log ("Hit A Enemy!");

                if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);

        } 

            if(hit.collider.gameObject.tag == "Target"){

                Debug.Log ("Target hit!");

                hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);

                hit.rigidbody.isKinematic = false;

                hit.rigidbody.useGravity = true;

                if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); 

            } else {            

            if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);

            Debug.Log ("Hit Other!");

        }

        ChangeWep = false;

        SendMessageUpwards("Recoil");

        shootEnabled = false;

        yield WaitForSeconds(shotInterval);

        shootEnabled = true;

        ChangeWep = true;    

        }

    }

c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: Weapon simulation using raycast?

Post by c6burns »

There's a raycast demo in the bullet samples ... AppRaytestDemo

There's also this wiki article: http://bulletphysics.org/mediawiki-1.5. ... ng_RayTest
gtafan
Posts: 14
Joined: Tue Jul 16, 2013 2:57 pm

Re: Weapon simulation using raycast?

Post by gtafan »

c6burns wrote:There's a raycast demo in the bullet samples ... AppRaytestDemo

There's also this wiki article: http://bulletphysics.org/mediawiki-1.5. ... ng_RayTest
And where can I find the bullet samples ... AppRaytestDemo?
c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: Weapon simulation using raycast?

Post by c6burns »

In the Demos folder of your bullet src ... inside the solution as separate projects if you use visual studio
Post Reply