Skip to content

Instantly share code, notes, and snippets.

@saturngamesss
Last active January 31, 2023 18:15
Show Gist options
  • Save saturngamesss/b5b5cc11b23c96028a7270fdbcda9f26 to your computer and use it in GitHub Desktop.
Save saturngamesss/b5b5cc11b23c96028a7270fdbcda9f26 to your computer and use it in GitHub Desktop.
Simple 2D grappling gun code for unity engine
//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;
}
}
}
@ewillett2024
Copy link

Ok I know I'm a moron and this is probably the most obvious solution ever, but how do I get the script to recognize my player character as the rigidbody? I have the script attached to my player but it doesn't work and comes up with an error stating that "'rigidbody2d' could not be found."

Once again, I know I'm probably an idiot and the solution is super simple, but I suck ass at coding and this is my first real attempt at making a game for a class.

@saturngamesss
Copy link
Author

Ok I know I'm a moron and this is probably the most obvious solution ever, but how do I get the script to recognize my player character as the rigidbody? I have the script attached to my player but it doesn't work and comes up with an error stating that "'rigidbody2d' could not be found."

Once again, I know I'm probably an idiot and the solution is super simple, but I suck ass at coding and this is my first real attempt at making a game for a class.

You need to add RigidBody2D component to your player.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment