The
if (!pushBack)
{
transform.Translate(Vector2.right * velocityX * Time.deltaTime);
transform.Translate(Vector2.up * velocityY * Time.deltaTime);
}
Is my entire walking/jumping system, I don’t know how else to make him walk… I realized I can instantly teleport by increasing velocityX, but this is of course, not ideal… Everything else I’ve attempted hasn’t worked, but if you need it here’s my entire script, though it’s kinda sloppy lol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public Magic[] Magics;
public CameraBoy cameraBoy;
public bool isGrounded, isLeft;
public Rigidbody2D rb;
[SerializeField]
private LayerMask Layer;
public LayerMask enemies;
public GameObject attackTrig;
public HeartMng heartMng;
public AttackBoy attackBoy;
public CameraShake.Properties properties;
public CameraShake shakey;
public PlayerBlock block;
public Camera mainCam;
public Transform attackPos;
public float attackRng, force;
public AudioSource[] jumpy;
public AudioSource[] ouch;
public AudioSource[] deado;
public AudioSource[] blockHit;
private int randomSound;
public static int coinBag, playerHealth, startHealth;
public static int curMaxHealth = 3;
public static int maxHealthEver = 6;
public float maxVelocity = 30;
public bool isAttacking, isDead, stopMove,pushBack;
public float walkSpeed = 7, speedSet;
public float gravity = -12;
public float jumpHeight;
private float screenHalfWidthInWorldUnits;
public Vector2 hitBox;
public float velocityY, velocityX;
public Animator animator;
void Awake()
{
cameraBoy = FindObjectOfType<CameraBoy>();
startHealth = curMaxHealth;
playerHealth = startHealth;
}
private void Start()
{
block = GetComponentInChildren<PlayerBlock>();
shakey = FindObjectOfType<CameraShake>();
attackBoy = GetComponent<AttackBoy>();
heartMng = FindObjectOfType<HeartMng>();
speedSet = walkSpeed;
hitBox = this.GetComponent<BoxCollider2D>().offset;
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("ground"))
{
isGrounded = true;
}
if (collision.gameObject.CompareTag("Boss"))
{
transform.Translate(Vector3.left / 2);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("ground"))
{
isGrounded = false;
}
}
public void Update()
{
if (stopMove)
{
velocityX = 0;
}
if (Input.GetKeyDown(KeyCode.F11)){
SceneManager.LoadScene(1);
}
if(playerHealth < 1)
{
playerHealth = 0;
Death();
}
velocityY = rb.velocity.y;
if (!isDead && !stopMove)
{
float inputX = Input.GetAxisRaw("Horizontal");
velocityX = inputX * walkSpeed;
}
if (Input.GetMouseButtonDown(1))
{
foreach (var spell in Magics)
{
}
}
if (Input.GetKeyDown(KeyCode.G))
{
shakey.StartShake(properties);
}
if(velocityX > 0)
{
isLeft = false;
this.GetComponent<BoxCollider2D>().offset = new Vector2(hitBox.x,hitBox.y);
attackPos.position = new Vector3(5.72f, 0, 0) + transform.position;
animator.SetBool("isRunning", true);
animator.SetBool("isLeft", false);
}
if (velocityX < 0)
{
isLeft = true;
this.GetComponent<BoxCollider2D>().offset = new Vector2(hitBox.x - 0.3f, hitBox.y);
attackPos.position = new Vector3(-5.72f, 0, 0) + transform.position;
animator.SetBool("isRunning",true);
animator.SetBool("isLeft", true);
}
else if(velocityX == 0)
{
animator.SetBool("isRunning", false);
}
if(velocityY < -0.5f)
{
isGrounded = false;
animator.speed = 1;
}
else if(velocityY > 0.5f)
{
isGrounded = false;
animator.speed = 0;
}
else if(velocityY == 0 && !attackBoy.charging)
{
animator.speed = 1;
}
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), velocityY);
Vector2 direction = input.normalized;
if (Input.GetKeyDown(KeyCode.W)&& !isDead && isGrounded && !stopMove)
{
Jump(jumpHeight);
}
if (Input.GetKeyUp(KeyCode.W) && !isDead)
{
if(velocityY > 0.1f)
{
rb.AddForce(new Vector2(0, -5000));
}
}
if (isGrounded == false)
{
animator.SetBool("isJumping", true);
}
else
{
animator.SetBool("isJumping", false);
}
if(transform.position.y < cameraBoy.minCamPos.y - 40)
{
Death();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Stomp")&& velocityY < -1f && collision.transform.parent.CompareTag("Enemy"))
{
collision.GetComponentInParent<EnemyCreator>().splatty = true;
collision.GetComponentInParent<EnemyCreator>().ReceiveDamage(1);
if (collision.GetComponentInParent<EnemyCreator>().eHealth >= 1)
{
Jump(jumpHeight / 3);
}
else
{
collision.GetComponent<BoxCollider2D>().isTrigger = true;
}
}
else if (collision.CompareTag("Stomp") && velocityY < -1f && collision.transform.parent.CompareTag("Boss"))
{
collision.GetComponentInParent<SnailBoss>().splatty = true;
collision.GetComponentInParent<SnailBoss>().ReceiveDamage(1);
if (collision.GetComponentInParent<SnailBoss>().eHealth >= 1)
{
Jump(jumpHeight / 2);
StartCoroutine(PushBack());
}
else
{
collision.GetComponent<BoxCollider2D>().isTrigger = true;
}
}
}
void FixedUpdate()
{
transform.Translate(Vector2.right * velocityX * Time.deltaTime);
transform.Translate(Vector2.up * velocityY * Time.deltaTime);
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxVelocity);
}
void Jump(float jumpSpeed)
{
randomSound = Random.Range(0, jumpy.Length);
rb.velocity = Vector2.up * jumpSpeed;
jumpy[randomSound].Play();
}
public void TakeDamage(int dmg, float force, Vector3 dir)
{
if (!block.blockHitter)
{
StartCoroutine(Damaged(dmg, force, dir));
}
}
public IEnumerator PushBack()
{
pushBack = true;
yield return new WaitForSeconds(0.2f);
print("Win");
velocityX = -100f;
yield return new WaitForSeconds(0.2f);
pushBack = false;
}
public IEnumerator Damaged(int dmg, float force,Vector3 dir)
{
if(dmg > playerHealth)
{
dmg = playerHealth;
}
heartMng.TakeDamage(dmg);
if (playerHealth > 0)
{
randomSound = Random.Range(0, ouch.Length);
ouch[randomSound].Play();
}
rb.AddForce(dir * force);
animator.SetBool("isDamaged", true);
yield return new WaitForSeconds(0.5f);
animator.SetBool("isDamaged", false);
}
public void Death()
{
if (!isDead)
{
shakey.StartShake(properties);
isDead = true;
randomSound = Random.Range(0, deado.Length);
deado[randomSound].Play();
playerHealth = 0;
animator.SetBool("isDead", true);
StartCoroutine(RestartLevel(3));
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(attackPos.position,attackRng);
}
public void CoinCollect(int coinNum)
{
coinBag += coinNum;
}
IEnumerator RestartLevel(int delay)
{
yield return new WaitForSeconds(delay);
string sceneName = SceneManager.GetActiveScene().name;
SceneManager.LoadScene(sceneName);
}
}