Planet Project Code
A downloadable tool
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public GameObject planet;
public Transform RotPoint;
public Transform jumpPoint;
public Rigidbody2D rb;
public Animator anim;
private float moveVal;
private float speedSave;
private float speedIncreaseSave;
public float speed;
public float speedIncrease;
public int jumpForce;
public bool moving = false;
public bool onGround = false;
private Vector2 jumpDir;
private Vector2 landDir;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
speedSave = speed;
speedIncreaseSave = speedIncrease;
}
public void PlayerMove()
{
float horz = Input.GetAxis("Horizontal");
if (onGround)
RotPoint.rotation = Quaternion.Euler(RotPoint.rotation.x, RotPoint.rotation.y, moveVal);
if (horz > 0 && Input.anyKey && onGround)
{
moveVal -= speed + speedIncrease * Time.deltaTime;
moving = true;
}
if (horz < 0 && Input.anyKey && onGround)
{
moveVal += speed + speedIncrease * Time.deltaTime;
moving = true;
}
if (horz == 0)
{
moving = false;
}
else
{
anim.Play("PlayerRun");
}
if (!moving)
{
speed = speedSave;
speedIncrease = speedIncreaseSave;
anim.Play("PlayerIdle");
}
}
public IEnumerator PlayerSprinting()
{
if (moving)
{
if (speedIncrease < 30)
{
speedIncrease += .1f;
yield return new WaitForSeconds(10000f);
}
}
}
public void PlayerJump()
{
if (Input.GetKeyDown(KeyCode.W) && onGround)
{
rb.AddForce(jumpDir * jumpForce);
}
if (!onGround)
{
rb.AddForce(landDir * 1.2f);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject != gameObject)
{
onGround = true;
planet = collision.gameObject;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject != gameObject)
{
onGround = false;
}
}
// Update is called once per frame
void Update()
{
PlayerMove();
PlayerJump();
StartCoroutine(PlayerSprinting());
if (planet != null)
{
jumpDir = transform.position - planet.transform.position;
landDir = jumpPoint.position - transform.position;
}
}
}
Leave a comment
Log in with itch.io to leave a comment.