Hi, I’ve never ever made a game before. I don’t understand C# as much as I would like to. I only know basic stuff like public class is making a variable and void update updates every frame. I’m making a project that I have been planning on for a year now. I sit down on my laptop and suddenly realize I don’t know how to code. Long story short, I need some help coding a grappling hook.
I have looked for tutorials online, but I’m scared that most of them might mess up my current code. And if they don’t mess it up, they usually aren’t what I want, like the grappling hook clinging on to the mouse. No, I want that when you fire the grappling hook, the needle at the end will stick to the surface of the platform so you can swing, kinda like Spiderman.
If anyone can help me, I’ll be very grateful.
My current code for basic platforming is:
Note that I got this from a tutorial and don’t understand what half of it means
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pcontroler : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float forcejump;
bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
public float rememberGroundedFor;
float lastTimeGrounded;
public int defaultAdditionalJumps = 1;
int additionalJumps;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Move();
Jump();
BetterJump();
CheckIfGrounded();
}
void Move()
{
float x = Input.GetAxisRaw ("Horizontal");
float moveBy = x * speed;
rb.velocity = new Vector2(moveBy, rb.velocity.y);
}
void Jump()
{
if(Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0))
{
rb.velocity = new Vector2(rb.velocity.x, forcejump);
additionalJumps--;
}
}
void CheckIfGrounded()
{
Collider2D colliders = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
if (colliders != null)
{
isGrounded = true;
additionalJumps = defaultAdditionalJumps;
}
else
{
if (isGrounded)
{
lastTimeGrounded = Time.time;
}
isGrounded = false;
}
}
void BetterJump()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
{
rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
}