Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views1 page

New Text Document

The document contains a C# script for player movement in a Unity game. It allows the player to move horizontally and jump, utilizing Rigidbody2D for physics interactions. The script manages jumping state based on collisions with the floor to prevent double jumping.

Uploaded by

dinesh bandara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

New Text Document

The document contains a C# script for player movement in a Unity game. It allows the player to move horizontally and jump, utilizing Rigidbody2D for physics interactions. The script manages jumping state based on collisions with the floor to prevent double jumping.

Uploaded by

dinesh bandara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour


{
public float speed;
public float jump;

private float Move;

public Rigidbody2D rb;

public bool isJumping;

// Start is called before the first frame update


void Start()
{

// Update is called once per frame


void Update()
{
Move = Input.GetAxis("Horizontal");

rb.velocity = new Vector2(speed * Move, rb.velocity.y);

if (Input.GetButtonDown("Jump") && isJumping == false)


{
rb.AddForce(new Vector2(rb.velocity.x, jump));
}
}

private void OnCollisionEnter2D(Collision2D other)


{
if(other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = true;
}
}
}

You might also like