From 53b2345edeb13b1b4036fc3a822353b718331833 Mon Sep 17 00:00:00 2001 From: LarsZauberer Date: Sat, 27 Feb 2021 18:46:14 +0100 Subject: [PATCH] Hook Dragging --- Assets/Prefabs/Hook.prefab | 6 ++++- Assets/Scripts/Objects/HookBehavior.cs | 35 +++++++++++++++++++++++++- Assets/Scripts/Player/Hook.cs | 10 +++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Assets/Prefabs/Hook.prefab b/Assets/Prefabs/Hook.prefab index a1552e3..8d805c0 100644 --- a/Assets/Prefabs/Hook.prefab +++ b/Assets/Prefabs/Hook.prefab @@ -143,5 +143,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b9cd6b288bec1cc43828ab4e54d3e60c, type: 3} m_Name: m_EditorClassIdentifier: - shootingSpeed: 10 + shootingSpeed: 50 dir: {x: 0, y: 0} + Hooked: 0 + pullSpeed: 125 + xPower: 2 + yPower: 0.5 diff --git a/Assets/Scripts/Objects/HookBehavior.cs b/Assets/Scripts/Objects/HookBehavior.cs index 8bfaeac..12b78a7 100644 --- a/Assets/Scripts/Objects/HookBehavior.cs +++ b/Assets/Scripts/Objects/HookBehavior.cs @@ -4,10 +4,12 @@ public class HookBehavior : MonoBehaviour { - public float shootingSpeed = 20f; + public float shootingSpeed = 100f; public Vector2 dir; private Rigidbody2D r; public bool Hooked = false; + public float pullSpeed = 100f; + public float xPower = 50f, yPower = 50f; // Start is called before the first frame update void Start() { @@ -19,6 +21,12 @@ void FixedUpdate() { if (!Hooked) { r.AddForce(dir*Time.fixedDeltaTime*shootingSpeed, ForceMode2D.Impulse); + } else { + GameObject player = GameObject.FindGameObjectWithTag("Player"); + Rigidbody2D rb = player.GetComponent(); + // rb.gravityScale = 0f; + + MovePlayer(); } } @@ -30,4 +38,29 @@ private void OnTriggerEnter2D(Collider2D other) { Destroy(gameObject); } } + + private void MovePlayer() { + GameObject player = GameObject.FindGameObjectWithTag("Player"); + Player script = player.GetComponent(); + Rigidbody2D rb = player.GetComponent(); + + Vector2 forceVector = CalculateDirection(transform.position, player.transform.position); + + Debug.Log(forceVector*Time.fixedDeltaTime*pullSpeed); + rb.AddForce(forceVector*Time.fixedDeltaTime*pullSpeed, ForceMode2D.Impulse); + } + + public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) { + Vector2 vec = new Vector2(); + vec.x = mouse.x-player.x; + vec.y = mouse.y-player.y; + + vec = vec/vec.magnitude; + + // x, y + power + vec.x *= xPower; + vec.y *= yPower; + + return vec; + } } diff --git a/Assets/Scripts/Player/Hook.cs b/Assets/Scripts/Player/Hook.cs index a141098..39e4957 100644 --- a/Assets/Scripts/Player/Hook.cs +++ b/Assets/Scripts/Player/Hook.cs @@ -18,7 +18,7 @@ void Update() public void Shoot() { if (obj) { - Destroy(obj); + deleteHook(); } mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); obj = Instantiate(hook) as GameObject; @@ -39,6 +39,14 @@ public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) { [Command()] public void deleteHook() { + Rigidbody2D playerRb = GameObject.FindGameObjectWithTag("Player").GetComponent(); + playerRb.gravityScale = 3f; Destroy(obj); } + + private void OnCollisionEnter2D(Collision2D other) { + if (other.gameObject.tag == "Targets") { + deleteHook(); + } + } }