Skip to content

Commit

Permalink
Hook not working
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsZauberer committed Mar 15, 2021
1 parent 53b2345 commit fba5bbf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Assets/Prefabs/UnstaticRect.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GameObject:
- component: {fileID: 9039277333789250394}
m_Layer: 0
m_Name: UnstaticRect
m_TagString: Untagged
m_TagString: Unstatic
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
Expand Down
53 changes: 28 additions & 25 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ GameObject:
- component: {fileID: 685362901}
- component: {fileID: 685362900}
- component: {fileID: 685362899}
- component: {fileID: 685362902}
m_Layer: 0
m_Name: Targets
m_TagString: Targets
Expand Down Expand Up @@ -1208,6 +1209,27 @@ Tilemap:
e31: 0
e32: 0
e33: 1
--- !u!50 &685362902
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 685362897}
m_BodyType: 2
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 1
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!1 &768009921
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -10524,6 +10546,12 @@ PrefabInstance:
value:
objectReference: {fileID: 5763763162042969811, guid: 4f82f4c1da74fbc4da7731dc62fb2165,
type: 3}
- target: {fileID: 2708059942275157637, guid: d83047ec43286764f8c30b46f65db4f0,
type: 3}
propertyPath: hookPrefab
value:
objectReference: {fileID: 5763763162042969811, guid: 4f82f4c1da74fbc4da7731dc62fb2165,
type: 3}
- target: {fileID: 2708059942275157883, guid: d83047ec43286764f8c30b46f65db4f0,
type: 3}
propertyPath: m_RootOrder
Expand Down Expand Up @@ -10939,41 +10967,16 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 9039277333789250394, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_Mass
value: 4
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250395, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_Name
value: UnstaticRect
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250395, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_Layer
value: 12
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250407, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_RootOrder
value: 5
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250407, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_LocalScale.x
value: 0.7458552
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250407, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_LocalScale.y
value: 0.7458552
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250407, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_LocalScale.z
value: 1.243092
objectReference: {fileID: 0}
- target: {fileID: 9039277333789250407, guid: f1b637442ecec244bab0518128285747,
type: 3}
propertyPath: m_LocalPosition.x
Expand Down
31 changes: 21 additions & 10 deletions Assets/Scripts/Objects/HookBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,54 @@ public class HookBehavior : MonoBehaviour
public Vector2 dir;
private Rigidbody2D r;
public bool Hooked = false;
public GameObject hookedToObj;
public float pullSpeed = 100f;
public float xPower = 50f, yPower = 50f;
public GameObject pullObject;
public bool allowedToPull = false;
// Start is called before the first frame update
void Start()
{
r = GetComponent<Rigidbody2D>();
pullObject = GameObject.FindGameObjectWithTag("Player");
}

// Update is called once per frame
void FixedUpdate()
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
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();
if (!player.GetComponent<Hook>().DoubleMech && allowedToPull) {
MoveObj();
}
}

if (player.GetComponent<Hook>().bothHooked()) {
MoveObj();
}
}

private void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Targets") {
if (other.tag == "Targets" || other.tag == "Unstatic") {
hookedToObj = other.gameObject;
Hooked = true;
r.velocity = new Vector2(0, 0);
} else if (other.tag != "Player" && other.tag != "MainCamera") {
Destroy(gameObject);
GameObject player = GameObject.FindGameObjectWithTag("Player");
Hook h = player.GetComponent<Hook>();
h.deleteHook();
}
}

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

Vector2 forceVector = CalculateDirection(transform.position, player.transform.position);
Vector2 forceVector = CalculateDirection(transform.position, pullObject.transform.position);

Debug.Log(forceVector*Time.fixedDeltaTime*pullSpeed);
rb.AddForce(forceVector*Time.fixedDeltaTime*pullSpeed, ForceMode2D.Impulse);
Expand Down
61 changes: 54 additions & 7 deletions Assets/Scripts/Player/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,53 @@
public class Hook : MonoBehaviour
{
private Vector3 mousePoint;
public GameObject hook;
public GameObject obj;
public GameObject hookPrefab;
public GameObject hook1;
public GameObject hook2;
public bool DoubleMech = false;

void Update()
{
if (Input.GetButtonDown("Fire1")) {
Shoot();
} else if (Input.GetButtonUp("Fire1") && hook1) {
HookBehavior hb = hook1.GetComponent<HookBehavior>();
if (hb.Hooked) {
DoubleMech = true;
ShootSecondHook();
} else {
hb.allowedToPull = true;
}
}

if (bothHooked()) {
setNewObj();
}
}

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

public void ShootSecondHook() {
HookBehavior hb1 = hook1.GetComponent<HookBehavior>();

// Create second hook
mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
hook2 = Instantiate(hookPrefab) as GameObject;
hook2.transform.position = transform.position;
HookBehavior hb2 = hook2.GetComponent<HookBehavior>();
hb2.dir = CalculateDirection(mousePoint, transform.position);
}

public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) {
Vector2 vec = new Vector2();
vec.x = mouse.x-player.x;
Expand All @@ -41,12 +67,33 @@ public Vector2 CalculateDirection(Vector3 mouse, Vector3 player) {
public void deleteHook() {
Rigidbody2D playerRb = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
playerRb.gravityScale = 3f;
Destroy(obj);
Destroy(hook1);
Destroy(hook2);
}

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

public bool bothHooked() {
if (!DoubleMech) {
return false;
}

HookBehavior hb1 = hook1.GetComponent<HookBehavior>();
HookBehavior hb2 = hook2.GetComponent<HookBehavior>();

return hb1.Hooked && hb2.Hooked;
}

public void setNewObj() {
// Set each others
HookBehavior hb1 = hook1.GetComponent<HookBehavior>();
HookBehavior hb2 = hook2.GetComponent<HookBehavior>();

hb1.pullObject = hb2.hookedToObj;
hb2.pullObject = hb1.hookedToObj;
}
}
1 change: 1 addition & 0 deletions ProjectSettings/TagManager.asset
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TagManager:
tags:
- Button
- Targets
- Unstatic
layers:
- Default
- TransparentFX
Expand Down

0 comments on commit fba5bbf

Please sign in to comment.