Skip to content

Instantly share code, notes, and snippets.

@Apearson75
Created December 22, 2021 10:57
Show Gist options
  • Save Apearson75/9b3eec5b123d8a3665c5fd1ca3b5dd18 to your computer and use it in GitHub Desktop.
Save Apearson75/9b3eec5b123d8a3665c5fd1ca3b5dd18 to your computer and use it in GitHub Desktop.
Player script for Dual Boot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
//Floating
public bool activateFloating = false;
public float horizontalSpeed;
public float verticalSpeed;
public float amplitude;
public Vector2 tempPosition;
private Rigidbody2D rb;
//Animations
public Animator anim;
//Movement
public float movementSpeed;
private bool facingRight = true;
public bool canMove = false;
public float jumpForce = 7;
//Health/Death
public float Health = 6f;
//Items
public bool hasGun = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
tempPosition = transform.position;
rb.freezeRotation = true;
}
// Update is called once per frame
void Update()
{
//Floating
if (activateFloating != false)
{
tempPosition.x = horizontalSpeed;
tempPosition.y = Mathf.Sin(Time.realtimeSinceStartup * verticalSpeed) * amplitude;
transform.position = tempPosition;
}
//Animations/Attacking
if (canMove)
{
if (Input.GetKeyDown(KeyCode.E))
{
anim.Play("Attack");
anim.GetComponent<Animator>().Play("Attack");
}
}
//Movement
if (canMove)
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * movementSpeed;
Vector3 charecterScale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
charecterScale.x = -1;
}
if (Input.GetAxis("Horizontal") > 0)
{
charecterScale.x = 1;
}
transform.localScale = charecterScale;
if(Input.GetButtonDown("Jump") && Mathf.Abs(rb.velocity.y) < 0.001f)
{
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
//Health/Death
if (Health <= 0)
{
SceneManager.LoadScene("Menu");
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//Health/Death
if (collision.collider.name == "BluePerson")
{
Health -= 1;
}
//Items
if(collision.collider.name == "Laser_Rifle_0")
{
Physics2D.IgnoreLayerCollision(8, 3, true);
hasGun = true;
}
}
private void OnCollisionStay2D(Collision2D collision)
{
//Attacking
if (Input.GetKeyDown(KeyCode.E) && collision.collider.name == "Dark_Blue_Guy")
{
GameObject darkbluePerson = collision.collider.gameObject;
ParticleSystem particleSystem = Instantiate(darkbluePerson.GetComponentInChildren<ParticleSystem>());
particleSystem.transform.position = darkbluePerson.transform.position;
particleSystem.Play();
Destroy(darkbluePerson);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment