Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.Networking;
- using UnityEngine.Networking.NetworkSystem;
- using UnityEngine;
- public class SCR_Axe_Throwing : NetworkBehaviour
- {
- //Adapted https://www.youtube.com/watch?v=vduSC2YHFnw
- //Adapted https://www.youtube.com/watch?v=RfnTIkfKRFI
- //Adapted https://answers.unity.com/questions/696579/getting-a-child-object-out-of-its-parent.html
- //Adapted https://docs.unity3d.com/Manual/AnimationParameters.html
- public GameObject Axe;
- [SerializeField] GameObject AxeRec;
- [SerializeField] GameObject playerCamera;
- //Gets the animator
- Animator animator;
- Animator axe_animator;
- private float AxeThrowingForce = 1000f;
- [SyncVar]
- private bool _HoldingAxe = true;
- //Returning
- private Transform trans;
- //Finds out it stuck to a wall.
- public static bool Stick = false;
- [SyncVar]
- public NetworkInstanceId parentNetId;
- //Refering to its start position and where it originates
- [SerializeField] GameObject Parent;
- //If clicked use the update function to bring the axe back
- private bool _Click;
- private bool _ToThrow;
- void Start()
- {
- if (isLocalPlayer)
- {
- Axe.GetComponent<Rigidbody>().useGravity = false;
- Axe.AddComponent<NetworkAnimator>().enabled = true;
- animator = GetComponent<Animator>();
- axe_animator = Axe.GetComponent<Animator>();
- Axe.GetComponent<NetworkAnimator>().animator = axe_animator;
- }
- }
- // Update is called once per frame
- void Update() {
- if (!isLocalPlayer)
- {
- return;
- }
- Throw_Update();
- if (_ToThrow)
- {
- animator.SetBool("To_Throw", true);
- }
- else
- {
- animator.SetBool("To_Throw", false);
- }
- }
- void Throw_Update()
- {
- if (_Click && Stick)
- {
- trans.position = Vector3.Lerp(trans.position, AxeRec.transform.position, Time.deltaTime * 5.5f); //Brings it to me
- trans.rotation = Quaternion.Lerp(trans.rotation, AxeRec.transform.rotation, Time.time * 5.5f); //Brings to me in the correct rotation
- }
- if (_HoldingAxe)
- {
- //Hovers in hand.
- Axe.transform.position = AxeRec.transform.position;
- Axe.transform.rotation = AxeRec.transform.rotation;
- if (Input.GetMouseButtonDown(0))
- {
- Axe.GetComponent<BoxCollider>().enabled = true;
- Throwing();
- }
- }
- if (_HoldingAxe == false)
- {
- if (Stick)
- {
- axe_animator.SetBool("_Thrown", false);
- trans = Axe.transform; //Trans possition is = to Axe Location
- if (Input.GetMouseButtonDown(0))
- {
- _Click = true; //Starts the movement return
- StartCoroutine(ReciveThrow()); // Sets it in hand
- }
- }
- }
- }
- void Throwing()
- {
- _ToThrow = true;
- StartCoroutine(To_Throw()); // Sets it in hand
- }
- IEnumerator ReciveThrow()
- {
- //Wait 2 seconds then set it as in hand
- yield return new WaitForSeconds(1);
- Cmd_ReciveThrow();
- }
- IEnumerator To_Throw()
- {
- //Wait 2 seconds then set it as in hand
- yield return new WaitForSeconds(1);
- Cmd_To_Throw();
- }
- [Command]
- public void Cmd_To_Throw()
- {
- _ToThrow = false;
- _HoldingAxe = false;
- Axe.GetComponent<Rigidbody>().useGravity = true;
- Axe.transform.parent = null;
- //Axe.GetComponent<Animation>().enabled = true;
- axe_animator.SetBool("_Thrown", true);
- Axe.GetComponent<Rigidbody>().AddForce(playerCamera.transform.forward * AxeThrowingForce); //Throws it
- Rpc_To_Throw();
- }
- [ClientRpc]
- public void Rpc_To_Throw()
- {
- _ToThrow = false;
- _HoldingAxe = false;
- Axe.GetComponent<Rigidbody>().useGravity = true;
- Axe.transform.parent = null;
- //Axe.GetComponent<Animation>().enabled = true;
- axe_animator.SetBool("_Thrown", true);
- Axe.GetComponent<Rigidbody>().AddForce(playerCamera.transform.forward * AxeThrowingForce); //Throws it
- }
- [Command]
- public void Cmd_ReciveThrow()
- {
- Axe.transform.SetParent(Parent.transform); // Makes Parent!
- Axe.GetComponent<Rigidbody>().useGravity = false; //Turns gravity off
- Axe.GetComponent<BoxCollider>().enabled = false; //Box Colliders on
- Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //Removes Contraints
- Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; //Removes Contraints
- _HoldingAxe = true;
- Stick = false;
- _Click = false;
- RPC_ReciveThrow();
- }
- [ClientRpc]
- public void RPC_ReciveThrow()
- {
- Axe.transform.SetParent(Parent.transform); // Makes Parent!
- Axe.GetComponent<Rigidbody>().useGravity = false; //Turns gravity off
- Axe.GetComponent<BoxCollider>().enabled = false; //Box Colliders on
- Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //Removes Contraints
- Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; //Removes Contraints
- _HoldingAxe = true;
- Stick = false;
- _Click = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement