Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Last active December 10, 2020 13:04
Show Gist options
  • Save TheCuttlefish/e1a9b6dba1f22fc66b531b73bf5efcde to your computer and use it in GitHub Desktop.
Save TheCuttlefish/e1a9b6dba1f22fc66b531b73bf5efcde to your computer and use it in GitHub Desktop.
Hook mechanic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
#region Variables
//movement + jump
public Transform feet;
float xSpeed = 10f;
public Rigidbody2D rb;
float xInput;
public LayerMask groundLayer;
public float jumpHeight = 10f;
public bool isGrounded = false;
//player art
public GameObject feetArt;
public GameObject bodyArt;
public GameObject headArt;
//points for neck
public Transform headPoint;
public Transform bodyPoint;
//mouse
Vector3 mousePos;
public GameObject cursor;
public bool isHooked = false;
Vector3 originalHeadPos;
public float pullSpeed = 0.1f;
//climbing
bool canClimb = false;
#endregion
private void Start()
{
originalHeadPos = headArt.transform.localPosition;
}
void Update()
{
//HookMechanic();
//Line();
Movement();
//FlipBasedOnDirection();
//Climbing();
}
public void HookMechanic()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
cursor.transform.position = mousePos;
Debug.DrawLine(headPoint.transform.position, cursor.transform.position, Color.red);
//connect
if (Input.GetMouseButtonDown(0) && cursor.GetComponent<CursorScript>().isActive)
{
Hook();
}
//disconnet
if (Input.GetMouseButtonDown(1))
{
Unhook();
}
if (isHooked)
{
if (headArt.GetComponent<DistanceJoint2D>().distance > 0)
{
headArt.GetComponent<DistanceJoint2D>().distance -= pullSpeed;
}
if (headArt.GetComponent<DistanceJoint2D>().distance < 0.2f)
{
Unhook();
}
}
}
public void Climbing()
{
if (canClimb)
{
rb.velocity -= (Vector2.up * -Input.GetAxis("Vertical") / 3);
}
}
public void Hook()
{
isHooked = true;
headArt.transform.position = cursor.transform.position;
headArt.transform.parent = null;
headArt.GetComponent<DistanceJoint2D>().enabled = true;
}
public void Unhook()
{
isHooked = false;
headArt.transform.parent = transform;
headArt.transform.localPosition = originalHeadPos;
headArt.GetComponent<DistanceJoint2D>().enabled = false;
}
public void Line()
{
GetComponent<LineRenderer>().SetPosition(0, headPoint.position);
GetComponent<LineRenderer>().SetPosition(1, bodyPoint.position);
}
public void Movement()
{
xInput = Input.GetAxis("Horizontal") * xSpeed;
if (isGrounded)
{
rb.velocity = new Vector2(xInput, rb.velocity.y);
}
else
{
rb.AddForce(new Vector2(xInput, 0));
}
isGrounded = Physics2D.OverlapBox(feet.position, feet.GetComponent<BoxCollider2D>().size, 0, groundLayer);
if (isGrounded && Input.GetButtonDown("Jump"))
{
rb.velocity = Vector2.up * jumpHeight;
}
}
public void FlipBasedOnDirection()
{
if (xInput > 0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
feetArt.transform.Rotate(0, 0, -xInput);
bodyArt.transform.localEulerAngles = new Vector3(0, 0, 50 + xInput * 3);
}
if (xInput < -0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
feetArt.transform.Rotate(0, 0, xInput);
bodyArt.transform.localEulerAngles = new Vector3(0, 0, 50 - xInput * 3);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform.tag == "Wall")
{
canClimb = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.transform.tag == "Wall")
{
canClimb = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment