0% found this document useful (0 votes)
47 views2 pages

Eski Kod

This script controls the movement of a 2D player character in Unity. It handles horizontal and vertical input, jumping, flipping the player's direction, and checking if the player is on the ground. The script gets references to the player's rigidbody and animator components. It updates each frame to move the player horizontally based on input, check if they are grounded, and allow jumping if conditions are met. It also contains methods to handle flipping the player and adding force to jump.

Uploaded by

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

Eski Kod

This script controls the movement of a 2D player character in Unity. It handles horizontal and vertical input, jumping, flipping the player's direction, and checking if the player is on the ground. The script gets references to the player's rigidbody and animator components. It updates each frame to move the player horizontally based on input, check if they are grounded, and allow jumping if conditions are met. It also contains methods to handle flipping the player and adding force to jump.

Uploaded by

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

using System;

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

public class PlayerMovement : MonoBehaviour


{
Rigidbody2D _playerRb;
Animator _playerAnimator;
public float moveSpeed = 1f;
public float jumpSpeed = 1f, jumpFruquency = 1f, nextJumpTime;
bool _facingRight = true;
public bool isGrounded = false;
public Transform groundCheckPosition;
public float groundCheckRadius;
public LayerMask groundChecklayer;

void Start()
{
_playerRb = GetComponent<Rigidbody2D>();
_playerAnimator = GetComponent<Animator>();
}

void Update()
{
HorizontalMove();
OnGroundCheck();

if (_playerRb.velocity.x < 0 && _facingRight)


{
FlipFace();
}
else if (_playerRb.velocity.x > 0 && !_facingRight)
{
FlipFace();
}

if (Input.GetAxis("Vertical") > 0 && isGrounded && (nextJumpTime <


Time.timeSinceLevelLoad))
{
nextJumpTime = Time.timeSinceLevelLoad + jumpFruquency;
Jump();
}

// if (Input.GetKeyDown(KeyCode.Space) == true)
// {
// Jump();
// }
}

// void FixedUpdate()
// {
// if (Input.GetAxis("Vertical") > 0 && isGrounded)
// {
// Jump();
// }
// }
void HorizontalMove()
{
_playerRb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed,
_playerRb.velocity.y);
_playerAnimator.SetFloat("playerSpeed", Mathf.Abs(_playerRb.velocity.x));
}

void FlipFace()
{
_facingRight = !_facingRight;
Vector3 tempLocalScale = transform.localScale;
tempLocalScale.x *= -1;
transform.localScale = tempLocalScale;
}

void Jump()
{
_playerRb.AddForce(new Vector2(0, jumpSpeed));
}

void OnGroundCheck()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPosition.position,
groundCheckRadius, groundChecklayer);
_playerAnimator.SetBool("isGroundedAnim", isGrounded);
}

// private void OnTriggerEnter(Collider other)


// {
// if (other.CompareTag("CheckPoint"))
// {
// throw new NotImplementedException();
// }
// }

//float moveSpeed = 10;


//float InputX = Input.GetAxis("Horizontal");
////Get the value of the Horizontal input axis.
//float InputY = Input.GetAxis("Vertical");
////Get the value of the Vertical input axis.
//transform.Translate(new Vector2(InputX, InputY) * moveSpeed *
Time.deltaTime);
////Move the object to XYZ coordinates defined as horizontalInput, 0, and
verticalInput respectively.
}

You might also like