Search Unity

Shoot at Centre

Discussion in 'Scripting' started by The3DKnight, Mar 17, 2013.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi .
    So i made a shooting script, and i have 2 problems, hope someone can help first is :
    When i shoot my Projectile it doesn't go to the crosshair. How can i make projectile go to crosshair.
    And second when my weapon goes trough wall and i shoot the projectiles go trough wall how can i make projectiles destroy when weapon is going trough wall?

    I don't understand raycast so i don't know how to use it. :(
    Here's my shooting script

    Code (csharp):
    1. //Shooting
    2. var BulletPrefab:Transform;
    3. var force : float = 2000;
    4. public var guiSkin : GUISkin;
    5.  
    6. //Rifle
    7. var ammo : int = 100;
    8. var FireRate = 0.1;
    9. var Rifle = true;
    10. var CanFire : boolean;
    11.  
    12. //Recoil
    13.  
    14. var crosshair : Texture2D;
    15.  
    16.  
    17.  
    18. function OnGUI () {
    19.  
    20. GUI.skin = guiSkin;
    21.    
    22. GUI.Label (Rect (100, 620, 100, 100),"Ammo :" + ammo);
    23.  
    24. GUI.Label(Rect(Screen.width/2 -25 ,Screen.height/2 -25 ,100,50), crosshair);
    25.  
    26. }
    27.  
    28. function Update()
    29. {
    30.  
    31. if(Input.GetButtonUp("Fire1"))
    32. {
    33. CanFire = true;
    34. }
    35.  
    36.  
    37.  
    38.  
    39. if (Rifle == false )
    40. {
    41.  
    42. if(Input.GetButtonDown("Fire1"))
    43.     {  
    44.     if (ammo >0)
    45.         {
    46.            
    47.             Fire();
    48.         }
    49.     }
    50. }
    51. else {
    52. if(Input.GetButton("Fire1"))
    53. {
    54.  
    55.  
    56.  
    57.     if (ammo >0)
    58. {
    59.    
    60.     Fire();
    61. }
    62. }
    63.         }
    64.  
    65.     }
    66.  
    67.  
    68. function Fire ()
    69. {
    70.  
    71.  
    72.  
    73. if(CanFire == true)
    74. {
    75.  
    76.     var bullet = Instantiate(BulletPrefab, GameObject.Find("spawnPoint").transform.position, GameObject.Find("Obj").transform.rotation);
    77.     bullet.rigidbody.AddForce(bullet.transform.forward * force);
    78.     CanFire = false;
    79.         yield WaitForSeconds (FireRate);
    80.     CanFire  = true;
    81.    
    82.     ammo -= 1;
    83.    
    84.    
    85.    
    86. }
    87.  
    88. }
    89.  
    I've searched a lot . But It's all raycast and i don't know how to use raycast.
    I'm sorry if i made any typing mistakes i'm so tired.

    Thanx for help.
     
  2. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
  3. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
  4. EF_ICYou

    EF_ICYou

    Joined:
    Jan 2, 2013
    Posts:
    18
    Raycast is easy to understand if you just try to use it.

    Basically, it's just an easy way to look if there is anything to collide with along a line.

    To quote your signature: Just believe in yourself and your abilities. You never know what you can do.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    think of raycast as a red dot laser sight for the unity engine... :)
    (I think it's more complicated to actually put a red dot laser sight into a FPS, it's just an analogy :p)

    you tell it where to start (i.e. the camera), which direction to go (i.e. camera rotation), how far to go (i.e. range/a very long way), and it tells you what it ran into.

    You can then have your gun rotate towards the point so when you fire a projectile it'll be aimed at that point.
     
    Last edited: Mar 20, 2013
  6. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Ty , it's more clear to me now . :D