Last active
December 5, 2023 18:27
-
-
Save SamatKadyrov/c1a9857e6260465c7fe7f5bf949e5e9f to your computer and use it in GitHub Desktop.
Minecraft and grenades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[RequireComponent(typeof(BoxCollider))] | |
public class Block : MonoBehaviour | |
{ | |
public void Destroy() | |
{ | |
Destroy(gameObject); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Builder : MonoBehaviour | |
{ | |
[SerializeField] private float _checkDistance; | |
[SerializeField] private Transform _raycastPoint; | |
[SerializeField] private Block _blockPrefab; | |
[SerializeField] private BuildPreview _buildPreview; | |
private RaycastHit _hitInfo; | |
private Vector3 BuildPosition => _hitInfo.transform.position + _hitInfo.normal; | |
private void Update() | |
{ | |
if (_hitInfo.transform == null) | |
return; | |
if (_hitInfo.transform.GetComponent<Block>() == null) | |
return; | |
if (Input.GetMouseButtonDown(0)) | |
Build(); | |
} | |
private void FixedUpdate() | |
{ | |
if(Physics.Raycast(_raycastPoint.position, _raycastPoint.forward, out _hitInfo, _checkDistance)) | |
{ | |
if (_buildPreview.IsActive == false) | |
{ | |
_buildPreview.Enable(); | |
} | |
_buildPreview.SetPosition(BuildPosition); | |
} | |
else if (_buildPreview.IsActive) | |
{ | |
_buildPreview.Disable(); | |
} | |
} | |
private void Build() | |
{ | |
Vector3 position = BuildPosition; | |
Instantiate(_blockPrefab, position, Quaternion.identity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class BuildPreview : MonoBehaviour | |
{ | |
public bool IsActive => gameObject.activeSelf; | |
public void Enable() | |
{ | |
gameObject.SetActive(true); | |
} | |
public void Disable() | |
{ | |
gameObject.SetActive(false); | |
} | |
public void SetPosition(Vector3 position) | |
{ | |
transform.position = position; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[RequireComponent(typeof(Rigidbody))] | |
public class Grenade : MonoBehaviour | |
{ | |
[SerializeField] private float _exposionRadius; | |
[SerializeField] private float _exposionDelay; | |
[SerializeField] private Rigidbody _rigidbody; | |
[SerializeField] private ParticleSystem _effect; | |
private void Update() | |
{ | |
if (_exposionDelay <= 0f) | |
Explode(); | |
_exposionDelay -= Time.deltaTime; | |
} | |
public void Throw(Vector3 force) | |
{ | |
_rigidbody.AddForce(force); | |
} | |
private void Explode() | |
{ | |
Collider[] hits = Physics.OverlapSphere(transform.position, _exposionRadius); | |
foreach(Collider hit in hits) | |
{ | |
if (hit.transform.TryGetComponent(out Block block)) | |
block.Destroy(); | |
} | |
Instantiate(_effect, transform.position, Quaternion.identity); | |
Destroy(gameObject); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class GrenadeThrower : MonoBehaviour | |
{ | |
[SerializeField] private Grenade _grenadePrefab; | |
[SerializeField] private float _throwForce; | |
[SerializeField] private Transform _throwPoint; | |
private void Update() | |
{ | |
if (Input.GetMouseButtonDown(1)) | |
{ | |
Instantiate(_grenadePrefab, _throwPoint.position, _throwPoint.rotation).Throw(_throwPoint.forward * _throwForce); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Looking : MonoBehaviour | |
{ | |
private readonly string MouseX = "Mouse X"; | |
private readonly string MouseY = "Mouse Y"; | |
[SerializeField] private float _speed; | |
[SerializeField] private Transform _camera; | |
[SerializeField] private Transform _body; | |
private void Update() | |
{ | |
_camera.Rotate(_speed * -Input.GetAxis(MouseY) * Time.deltaTime * Vector3.right); | |
_body.Rotate(_speed * Input.GetAxis(MouseX) * Time.deltaTime * Vector3.up); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Movement : MonoBehaviour | |
{ | |
private readonly string Horizontal = "Horizontal"; | |
private readonly string Vertical = "Vertical"; | |
[SerializeField] private float _speed; | |
private void Update() | |
{ | |
Vector3 direction = new Vector3(Input.GetAxis(Horizontal), 0f, Input.GetAxis(Vertical)); | |
transform.Translate(_speed * Time.deltaTime * direction); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment