Skip to content

Commit

Permalink
Hook Dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsZauberer committed Feb 27, 2021
1 parent bb2c731 commit 53b2345
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Assets/Prefabs/Hook.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 34 additions & 1 deletion Assets/Scripts/Objects/HookBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<Rigidbody2D>();
// rb.gravityScale = 0f;

MovePlayer();
}
}

Expand All @@ -30,4 +38,29 @@ private void OnTriggerEnter2D(Collider2D other) {
Destroy(gameObject);
}
}

private void MovePlayer() {
GameObject player = GameObject.FindGameObjectWithTag("Player");
Player script = player.GetComponent<Player>();
Rigidbody2D rb = player.GetComponent<Rigidbody2D>();

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;
}
}
10 changes: 9 additions & 1 deletion Assets/Scripts/Player/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +39,14 @@ public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) {

[Command()]
public void deleteHook() {
Rigidbody2D playerRb = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
playerRb.gravityScale = 3f;
Destroy(obj);
}

private void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "Targets") {
deleteHook();
}
}
}

0 comments on commit 53b2345

Please sign in to comment.