Showing Progress While Loading Scene | Unity Game Engine
SceneLoader.cs
using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class SceneLoader : MonoBehaviour { public GameObject LoaderUI; public Slider progressSlider; public void LoadScene(int index) { StartCoroutine(LoadScene_Coroutine(index)); } public IEnumerator LoadScene_Coroutine(int index) { progressSlider.value = 0; LoaderUI.SetActive(true); AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(1); asyncOperation.allowSceneActivation = false; float progress = 0; while (!asyncOperation.isDone) { progress = Mathf.MoveTowards(progress, asyncOperation.progress, Time.deltaTime); progressSlider.value = progress; if (progress >= 0.9f) { progressSlider.value = 1; asyncOperation.allowSceneActivation = true; } yield return null; } } }
Player Movement using Character Controller(Move, Turn, Jump, Fall) | Unity Game Engine
PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(CharacterController))] public class PlayerController : MonoBehaviour { public float speed = 3; public float rotationSpeed = 90; public float gravity = -20f; public float jumpSpeed = 15; CharacterController characterController; Vector3 moveVelocity; Vector3 turnVelocity; void Awake() { characterController = GetComponent<CharacterController>(); } void Update() { var hInput = Input.GetAxis("Horizontal"); var vInput = Input.GetAxis("Vertical"); if(characterController.isGrounded) { moveVelocity = transform.forward * speed * vInput; turnVelocity = transform.up * rotationSpeed * hInput; if(Input.GetButtonDown("Jump")) { moveVelocity.y = jumpSpeed; } } //Adding gravity moveVelocity.y += gravity * Time.deltaTime; characterController.Move(moveVelocity * Time.deltaTime); transform.Rotate(turnVelocity * Time.deltaTime); } }
Shooting Laser using Raycast and LineRenderer | Unity Game Engine
RaycastGun.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(LineRenderer))] public class RaycastGun : MonoBehaviour { public Camera playerCamera; public Transform laserOrigin; public float gunRange = 50f; public float fireRate = 0.2f; public float laserDuration = 0.05f; LineRenderer laserLine; float fireTimer; void Awake() { laserLine = GetComponent<LineRenderer>(); } void Update() { fireTimer += Time.deltaTime; if(Input.GetButtonDown("Fire1") && fireTimer > fireRate) { fireTimer = 0; laserLine.SetPosition(0, laserOrigin.position); Vector3 rayOrigin = playerCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if(Physics.Raycast(rayOrigin, playerCamera.transform.forward, out hit, gunRange)) { laserLine.SetPosition(1, hit.point); Destroy(hit.transform.gameObject); } else { laserLine.SetPosition(1, rayOrigin + (playerCamera.transform.forward * gunRange)); } StartCoroutine(ShootLaser()); } } IEnumerator ShootLaser() { laserLine.enabled = true; yield return new WaitForSeconds(laserDuration); laserLine.enabled = false; } }
Subscribe to:
Posts (Atom)