Hey. I'm trying to write script for gun. But it's not working, becouse there is 3 errors. Code: Code (csharp): var Zero = 0; var Clip = 8; var Bullet : GameObject; var Spawn : GameObject; var BulletSpeed : float = 100.0; function Update(){ if(Input.GetButtonDown("Fire1")){ if (Clip > Zero){ Fire(); yield WaitForSeconds(0.5); } } if (Clip = Zero){ Reolad(); } } function Fire(){ var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation); bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed); clip - 1 } function Reolad(){ yield WaitForSeconds(2); Clip + 8 } Errors: Code (csharp): Assets/skrypty/new springfield.js(17,14): BCE0044: expecting :, found ';'. Assets/skrypty/new springfield.js(16,20): BCE0043: Unexpected token: Zero. Assets/skrypty/new springfield.js(16,18): BCE0044: expecting ), found '='.
Code (csharp): if (Clip = Zero){ needs to be Code (csharp): if (Clip == Zero){ Equality comparison always is == (or ===). Plus you should make sure to always give a type to your variables, like Code (csharp): var Clip : int = 8; It's also common practice to give variables a camelCased name starting with a lowercase letter, and a method a camelCased name starting with uppercase letter. Just to make sure that you are able to distinguish both at any given time: Code (csharp): var bulletSpeed: float = 100.0; function ReloadWeapon() { }
Now there is no errors. Code: Code (csharp): var Zero : int = 0; var Clip : int = 8; var Bullet : GameObject; var Spawn : GameObject; var BulletSpeed : float = 100.0; function Update(){ if(Input.GetButtonDown("Fire1")){ if (Clip > Zero){ Fire(); yield WaitForSeconds(0.5); Clip --; } } if (Clip == Zero){ ReoladWeapon(); } } function Fire(){ var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation); bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed); } function ReoladWeapon(){ yield WaitForSeconds(2); Clip += 8; } But it no worka that as i want. It should look that: -shot, -bulet -1 -wait (yield) -next shot -if clip =00, stop -reolad What is wrong?
you can't use waitforseconds within the Update function... you can only yield within the coroutine functions themselves. check out: http://forum.unity3d.com/threads/73101-Function-Update-and-yield-WaitForSeconds ps try to layout your code a little more regularly, it really helps with the readability and makes it simpler to spot mistakes and errors. For example, the update function from above should be laid out a little like this (haven't fixed it, just changed the layout and formatting): Code (csharp): function Update() { if(Input.GetButtonDown("Fire1")) { if (Clip > Zero) { Fire(); yield WaitForSeconds(0.5); Clip --; } } if (Clip == Zero) { ReoladWeapon(); } }
Now is look that: Code (csharp): var Zero : int = 0; var Clip : int = 8; var Bullet : GameObject; var Spawn : GameObject; var BulletSpeed : float = 100.0; function Update(){ if(Input.GetButtonDown("Fire1")){ if (Clip > Zero){ Fire(); MyUpdate1(); Clip --; } } if (Clip == Zero){ ReoladWeapon(); } } function Fire(){ var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation); bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed); } function ReoladWeapon(){ MyUpdate2(); Clip += 8; } function MyUpdate1(){ yield WaitForSeconds(0.5); } function MyUpdate2(){ yield WaitForSeconds(2); } Work as before.
Code (csharp): var Clip : int = 8; var Bullet : GameObject; var Spawn : GameObject; var BulletSpeed : float = 100.0; var nextFireTime : float; var RateOfFire : float = 0.5f; function Start() { nextFireTime = Time.time + RateOfFire; } function Update() { if (Input.GetButtonDown("Fire1") Time.time > nextFireTime Clip > 0) { Fire(); nextFireTime = Time.time + RateOfFire; Clip--; } if (Clip <= 0) { StartCoroutine(ReoladWeapon()); } } function Fire() { var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation); bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed); } function ReoladWeapon() { yield WaitForSeconds(2f); Clip += 8; } This is how it should be done
Hi. Now J have workin script for weapon. Script throw bullet from spawn poin. Game will be Third Person Shooter. Crosshair will be in the midle of the screen. Player, weapon and spawn point on the left corner. I'm thinking about aiming system. Maybe Main Camera creat target point on first surface and spawn point will be turn on it?