Search Unity

Projectile with raycast

Discussion in 'Scripting' started by KingLlama, Aug 1, 2015.

  1. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    I have a simple ray cast script and I want to attach a bullet it to it and a reload function. How would I go about doing that? Would be something similar to a melee script with animations but adding a requirement for reload after firing?I'll add my weapon raycast script when I get home to show you what I have so far!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You want to attach a raycast to the bullet or you want to shoot a bullet after a raycast check? And where does the reload come in? Your question is pretty vague. Try rewording it.
     
  3. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    So what my goal is to have the weapon attached to the script to be able to shoot with r but with the bullet moving with the raycast and then to check if the weapon has a bullet if not it would needed to be reloaded with a timer and a animation attached.

    Is that better wording? I'm sorry i'm just really new to this. Here is my script so far.


    Code (JavaScript):
    1.  
    2. var Effect : Transform;
    3. var TheDamage = 100;
    4.  
    5. function Update ()
    6. {
    7.    
    8.     var hit : RaycastHit;
    9.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    10.    
    11.     if (Input.GetMouseButtonDown(0))
    12.     {
    13.         if (Physics.Raycast (ray, hit, 100))
    14.         {
    15.             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
    16.             Destroy(particleClone.gameObject, 2);
    17.             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    18.         }
    19.     }
    20.    
    21. }
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It's definitely more clear now, thank you.

    What aspect are you struggling with? All of it? It seems like you have most of the logic sorted, except the timing aspect.

    For the ammo, just make another variable at the top called ammo and give it a starting value (say 10) and decrease it every time you shoot. If it's <= 0, start playing your animation and don't let the player shoot any more until that's done.
     
  5. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    Alright, the next step is adding the bullet though to the weapon to shoot when I left click. I'm learning by tutorials each day so it becomes more understandable each time.

    So for the ammo var ammo = 10; then I get lost on what to add it too.....
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You can store it on the gun, on the person, on the backpack- etc...

    Personally, I think realism lends itself quite well to the problem. The overall ammo that your player holds should be held on your player/inventory object really, like a general "pool", then when you "reload" a gun, the pool should drop by the max number of bullets the weapon can hold, which should "refill" the weapon. While firing, it's the gun's own ammo variable that falls, and hitting zero would maybe switch animations to "firing but nothing happening", or simply not allow you to fire at all, or automatically reload (depending on your preference).