Unity 5.x By Example by Alan Thorn (Packt Publishing 2016)
Author:Alan Thorn (Packt Publishing, 2016)
Language: eng
Format: epub, mobi
Publisher: Packt Publishing
To use this class, create a new GameObject in the scene called AmmoManager by selecting GameObject | Create Empty from the application menu. Then, drag and drop the AmmoManager script from the Project panel to select the object in the scene. Once created, drag and drop the Ammo prefab from the Prefabs folder to the Ammo Prefab slot for the Ammo Manager component in the Object Inspector. See Figure 4.12:
Figure 4.12: Adding the Ammo Manager to an object
Now, the scene features an AmmoManager object to hold an ammo pool, offscreen and hidden. However, still nothing about our existing functionality actually connects a fire button press from the gamer with the generation of ammo in the scene. That is, we have no code to actually make the ammo visible and working! This connection should now be made via the PlayerController script that we started in the previous chapter. This class should now be amended to handle ammo generation. The recoded PlayerController class is included in the following Code Sample 4.3. The amendments are highlighted:
//------------------------------ using UnityEngine; using System.Collections; //------------------------------ public class PlayerController : MonoBehaviour { //------------------------------ private Rigidbody ThisBody = null; private Transform ThisTransform = null; public bool MouseLook = true; public string HorzAxis = "Horizontal"; public string VertAxis = "Vertical"; public string FireAxis = "Fire1"; public float MaxSpeed = 5f; public float ReloadDelay = 0.3f; public bool CanFire = true; public Transform[] TurretTransforms; //------------------------------ // Use this for initialization void Awake () { ThisBody = GetComponent<Rigidbody>(); ThisTransform = GetComponent<Transform>(); } //------------------------------ // Update is called once per frame void FixedUpdate () { //Update movement float Horz = Input.GetAxis(HorzAxis); float Vert = Input.GetAxis(VertAxis); Vector3 MoveDirection = new Vector3(Horz, 0.0f, Vert); ThisBody.AddForce(MoveDirection.normalized * MaxSpeed); //Clamp speed ThisBody.velocity = new Vector3(Mathf.Clamp(ThisBody.velocity.x, -MaxSpeed, MaxSpeed), Mathf.Clamp(ThisBody.velocity.y, -MaxSpeed, MaxSpeed), Mathf.Clamp(ThisBody.velocity.z, -MaxSpeed, MaxSpeed)); //Should look with mouse? if(MouseLook) { //Update rotation - turn to face mouse pointer Vector3 MousePosWorld = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f)); MousePosWorld = new Vector3(MousePosWorld.x, 0.0f, MousePosWorld.z); //Get direction to cursor Vector3 LookDirection = MousePosWorld - ThisTransform.position; //FixedUpdate rotation ThisTransform.localRotation = Quaternion.LookRotation(LookDirection.normalized,Vector3.up); } //Check fire control if(Input.GetButtonDown(FireAxis) && CanFire) { foreach(Transform T in TurretTransforms) AmmoManager.SpawnAmmo(T.position, T.rotation); CanFire = false; Invoke ("EnableFire", ReloadDelay); } } //------------------------------ void EnableFire() { CanFire = true; } //------------------------------ public void Die() { Destroy(gameObject); } } //------------------------------
Download
Unity 5.x By Example by Alan Thorn (Packt Publishing 2016).mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(26291)
Hello! Python by Anthony Briggs(25216)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(24447)
Kotlin in Action by Dmitry Jemerov(23536)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(22881)
Dependency Injection in .NET by Mark Seemann(22668)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21432)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20273)
Grails in Action by Glen Smith Peter Ledbrook(19343)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17056)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16366)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14077)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12255)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11533)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10645)
Hit Refresh by Satya Nadella(9223)
The Kubernetes Operator Framework Book by Michael Dame(8579)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8432)
Robo-Advisor with Python by Aki Ranin(8376)