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): //Shooting var BulletPrefab:Transform; var force : float = 2000; public var guiSkin : GUISkin; //Rifle var ammo : int = 100; var FireRate = 0.1; var Rifle = true; var CanFire : boolean; //Recoil var crosshair : Texture2D; function OnGUI () { GUI.skin = guiSkin; GUI.Label (Rect (100, 620, 100, 100),"Ammo :" + ammo); GUI.Label(Rect(Screen.width/2 -25 ,Screen.height/2 -25 ,100,50), crosshair); } function Update() { if(Input.GetButtonUp("Fire1")) { CanFire = true; } if (Rifle == false ) { if(Input.GetButtonDown("Fire1")) { if (ammo >0) { Fire(); } } } else { if(Input.GetButton("Fire1")) { if (ammo >0) { Fire(); } } } } function Fire () { if(CanFire == true) { var bullet = Instantiate(BulletPrefab, GameObject.Find("spawnPoint").transform.position, GameObject.Find("Obj").transform.rotation); bullet.rigidbody.AddForce(bullet.transform.forward * force); CanFire = false; yield WaitForSeconds (FireRate); CanFire = true; ammo -= 1; } } 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.
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.
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 ) 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.