Hi all, I am new to c# and unity. Im trying to have my player run and jump and maintain that speed and the player doesn’t seem to jump while I hold the run button. also when the player is touching the rope it doest stay attached. Some help would be nice. here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Ground logic
public bool groundTouch;
public Transform GroundCheck;
public LayerMask groundLayerMask;
//Wall logic
public bool wallTouch;
public Transform WallCheck;
public LayerMask wallLayerMask;
//Rope logic
public bool ropeTouch;
public Transform RopeCheck;
public LayerMask ropeLayerMask;
//player attributes
public bool canDoubleJump;
//rigidbody
private Rigidbody2D rb;
//walking
public float speed = 50f;
public float maxSpeed = 3;
//running
public float runSpeed = 100f;
public float maxRunSpeed = 5;
//jumping
public float jumpPower = 200f;
//climbing
public float climbSpeed = 10f;
public float swingSpeed = 10f;
//direction
public bool facingRight;
//Interactive objects
private Rigidbody2D rope;
void Start()
{
//Initialize rigidbodies
rb = gameObject.GetComponent<Rigidbody2D>();
rope = GameObject.FindGameObjectWithTag("Rope").GetComponent<Rigidbody2D>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//check if player is touching something
groundTouch = Physics2D.OverlapCircle(GroundCheck.position, 0.1f, groundLayerMask);
wallTouch = Physics2D.OverlapCircle(WallCheck.position, 0.1f, wallLayerMask);
ropeTouch = Physics2D.OverlapCircle(RopeCheck.position, 0.1f, ropeLayerMask);
//turning left and right
if (Input.GetAxis("Horizontal") < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
facingRight = false;
}
if (Input.GetAxis("Horizontal") > 0)
{
transform.localScale = new Vector3(1, 1, 1);
facingRight = true;
}
//Jumping
if (Input.GetButtonDown("Jump"))
{
if (groundTouch)
{
rb.AddForce(Vector2.up * jumpPower);
canDoubleJump = true;
}
else
{
if (canDoubleJump)
{
canDoubleJump = false;
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.AddForce(Vector2.up * jumpPower);
}
}
}
//climbing
if (RopeCheck)
{
//player to grab rope when touching it automatically
//horizontal axis h to swing back and forth
//vertical axis v to climb up and down
//jump off
if (ropeTouch)
{
Vector2 localPos = transform.localPosition;
localPos.x = 0;
rb.gravityScale = 0;
rb.AddForce((Vector2.right * swingSpeed) * h);
rb.AddForce((Vector2.up) * v);
}
else
{
rb.gravityScale = 1;
}
}
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//move player and control air movement
if (groundTouch)
{
//walking
rb.AddForce((Vector2.right * speed) * h);
//running
if(Input.GetKey("h") && Input.GetKey("d"))
{
rb.AddForce((Vector2.right * runSpeed));
}
else if(Input.GetKey("h") && Input.GetKey("a"))
{
rb.AddForce((-Vector2.right * runSpeed));
}
}
//limit speed
if (rb.velocity.x > maxSpeed)
{
rb.velocity = new Vector2(maxSpeed, rb.velocity.y);
}
if (rb.velocity.x < -maxSpeed)
{
rb.velocity = new Vector2(-maxSpeed, rb.velocity.y);
}
//limit runSpeed
if (rb.velocity.x > maxRunSpeed)
{
rb.velocity = new Vector2(maxRunSpeed, rb.velocity.y);
}
if (rb.velocity.x < -maxRunSpeed)
{
rb.velocity = new Vector2(-maxRunSpeed, rb.velocity.y);
}
}
}