Last active
January 31, 2023 18:15
-
-
Save saturngamesss/b5b5cc11b23c96028a7270fdbcda9f26 to your computer and use it in GitHub Desktop.
Simple 2D grappling gun code for unity engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//FROM PROJECT ROROPE || REAL GAMES STUDIO | |
//************************************************ | |
//realgamesss.weebly.com | |
//gamejolt.com/@Real_Game | |
//realgamesss.newgrounds.com/ | |
//real-games.itch.io/ | |
//youtube.com/channel/UC_Adg-mo-IPg6uLacuQCZCQ | |
//************************************************ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GrapplingGun : MonoBehaviour | |
{ | |
DistanceJoint2D rope; | |
bool checker; | |
void Start() | |
{ | |
gameObject.AddComponent<LineRenderer>(); | |
gameObject.AddComponent<RigidBody2D>(); | |
} | |
void Update() | |
{ | |
// Detect mouse position | |
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); | |
// Shot rope on mouse position | |
if (Input.GetMouseButtonDown(0) && checker == true) | |
{ | |
rope = gameObject.AddComponent<DistanceJoint2D>(); | |
rope.connectedAnchor = mousePos; | |
checker = false; | |
} | |
// Destroy rope | |
else if (Input.GetMouseButtonDown(0)) | |
{ | |
DestroyImmediate(rope); | |
checker = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to add RigidBody2D component to your player.