Skip to content

Commit

Permalink
Hook Single Object Behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsZauberer committed Feb 27, 2021
1 parent 147ea36 commit c13170b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
15 changes: 15 additions & 0 deletions Assets/Prefabs/Hook.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GameObject:
- component: {fileID: 6167312168718169834}
- component: {fileID: -7018169061865207225}
- component: {fileID: -8534197892154159958}
- component: {fileID: -3774645765217698002}
m_Layer: 0
m_Name: Hook
m_TagString: Untagged
Expand Down Expand Up @@ -130,3 +131,17 @@ BoxCollider2D:
serializedVersion: 2
m_Size: {x: 1.632, y: 1.392}
m_EdgeRadius: 0
--- !u!114 &-3774645765217698002
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5763763162042969811}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b9cd6b288bec1cc43828ab4e54d3e60c, type: 3}
m_Name:
m_EditorClassIdentifier:
shootingSpeed: 10
dir: {x: 0, y: 0}
21 changes: 21 additions & 0 deletions Assets/Scripts/Objects/HookBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HookBehavior : MonoBehaviour
{
public float shootingSpeed = 20f;
public Vector2 dir;
private Rigidbody2D r;
// Start is called before the first frame update
void Start()
{
r = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
r.AddForce(dir*Time.fixedDeltaTime*shootingSpeed, ForceMode2D.Impulse);
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Objects/HookBehavior.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 3 additions & 19 deletions Assets/Scripts/Player/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@
public class Hook : MonoBehaviour
{
private Vector3 mousePoint;
public Vector2 dir;
public GameObject hook;
public GameObject obj;
public float shootingSpeed = 10f;
// Start is called before the first frame update
void Start()
{
Update();
}

// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1")) {
Shoot();
}
}

private void FixedUpdate() {
if (obj) {
Rigidbody2D r = obj.GetComponent<Rigidbody2D>();
// r.isKinematic = false;
r.AddForce(CalculateDirection(mousePoint, transform.position)*Time.fixedDeltaTime*shootingSpeed, ForceMode2D.Impulse);
// r.AddForce(GravityVector, ForceMode2D.Impulse);
}
}

public void Shoot() {
mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
obj = Instantiate(hook) as GameObject;
GameObject obj = Instantiate(hook) as GameObject;
obj.transform.position = transform.position;
HookBehavior hb = obj.GetComponent<HookBehavior>();
hb.dir = CalculateDirection(mousePoint, transform.position);
}

public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) {
Expand Down

0 comments on commit c13170b

Please sign in to comment.