Created
June 22, 2023 06:16
-
-
Save adadesions/577a16d762f8d95de8bbada0062cdea9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System; | |
| using Games.MiniGame.GCD_Game.Scripts.Items; | |
| using Paideria.Minigames.GCD; | |
| using UnityEngine; | |
| namespace Games.MiniGame.GCD_Game.Scripts.Player | |
| { | |
| public class PlayerGrabItem : MonoBehaviour, IGrabItemByPlayer | |
| { | |
| [SerializeField] private Vector3 offset; | |
| [SerializeField] Vector3 _throwingForce; | |
| [SerializeField] float _launchAngle = 45.0f; | |
| private float _launchAngleRad; | |
| private Vector3 _throwVelocity; | |
| private GameObject _holdingItem; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| _holdingItem = null; | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| ItemMovingWithPlayer(); | |
| } | |
| private void ItemMovingWithPlayer() | |
| { | |
| if (_holdingItem == null) return; | |
| try { | |
| bool isHolding = _holdingItem.GetComponent<GrabableItem>().IsHolding; | |
| if (_holdingItem && isHolding) { | |
| _holdingItem.transform.position = transform.position + offset; | |
| } | |
| } catch (Exception) { | |
| _holdingItem = null; | |
| } | |
| } | |
| public void PlayerHoldItem(GameObject item) | |
| { | |
| _holdingItem = item; | |
| _holdingItem.transform.position = transform.position + offset; | |
| } | |
| public void PlayerThrowItem() | |
| { | |
| _launchAngleRad = _launchAngle * Mathf.Deg2Rad; | |
| _throwVelocity = new Vector3( | |
| _throwingForce.z * Mathf.Cos(_launchAngleRad), | |
| _throwingForce.y * Mathf.Sin(_launchAngleRad), | |
| 0f | |
| ); | |
| var rb = _holdingItem.GetComponent<Rigidbody>(); | |
| var horizontalComponent= _throwingForce.z * transform.forward; | |
| var verticalComponent = _throwingForce.y * Vector3.up; | |
| var forceVector = horizontalComponent + verticalComponent; | |
| rb.AddForce(forceVector, ForceMode.Impulse); | |
| _holdingItem = null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment