Base done

This commit is contained in:
Gerard Gascón 2023-08-09 20:07:47 +02:00
parent 32f5ed6299
commit 6ba30e430e
16 changed files with 828 additions and 42 deletions

47
Assets/Scripts/Enemy1.cs Normal file
View file

@ -0,0 +1,47 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy1 : MonoBehaviour {
[SerializeField] float speed;
[SerializeField] LayerMask groundLayer;
[Header("Flipping Pivots")]
[SerializeField] Transform flippingPivotLeft;
[SerializeField] Transform flippingPivotRight;
int _facingDirection = 1;
Rigidbody2D _rb;
// Start is called before the first frame update
void Awake() {
_rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate() {
if (_facingDirection > 0) {
if (Physics2D.OverlapBox(flippingPivotRight.position, flippingPivotRight.localScale, 0, groundLayer)) {
_facingDirection = -1;
transform.localScale = new Vector3(-1, 1, 1);
}
}else {
if (Physics2D.OverlapBox(flippingPivotLeft.position, flippingPivotLeft.localScale, 0, groundLayer)) {
_facingDirection = 1;
transform.localScale = Vector3.one;
}
}
_rb.velocity = new Vector2(speed * _facingDirection, _rb.velocity.y);
}
void OnDrawGizmos() {
if(!flippingPivotLeft || !flippingPivotRight) return;
Gizmos.color = Color.red;
Gizmos.DrawWireCube(flippingPivotLeft.position, flippingPivotLeft.localScale);
Gizmos.DrawWireCube(flippingPivotRight.position, flippingPivotRight.localScale);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: de0543dd18c7f9945a8e617c8a7a8f30
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,20 @@
using System;
using UnityEngine;
public class EnemyWeakPoint1 : MonoBehaviour {
Enemy1 _enemy;
void Awake() {
_enemy = GetComponentInParent<Enemy1>();
}
void OnTriggerEnter2D(Collider2D other) {
if (!other.CompareTag("Player")) return;
PlayerMovement1 player = other.GetComponent<PlayerMovement1>();
player.Rb.velocity = new Vector2(player.Rb.velocity.x, player.deathForce);
player.Animator.SetTrigger(PlayerMovement1.Jump1);
Destroy(_enemy.gameObject);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6dbb9bbb794a4ef0acd40077f0ccb5b3
timeCreated: 1691603524

View file

@ -4,6 +4,7 @@ using UnityEngine.InputSystem;
public class PlayerMovement1 : MonoBehaviour {
public float deathForce = 8f;
[SerializeField] float speed = 5f;
[SerializeField] float jumpForce = 10f;
@ -19,20 +20,22 @@ public class PlayerMovement1 : MonoBehaviour {
bool _grounded;
int _facingDirection = 1;
Rigidbody2D _rb;
public Rigidbody2D Rb { get; private set; }
PlayerInput _playerInput;
[Header("Animations")]
[SerializeField] Animator animator;
public Animator Animator => animator;
static readonly int Jump1 = Animator.StringToHash("Jump");
public static readonly int Jump1 = Animator.StringToHash("Jump");
static readonly int XVelocity = Animator.StringToHash("xVelocity");
static readonly int YVelocity = Animator.StringToHash("yVelocity");
static readonly int Grounded = Animator.StringToHash("Grounded");
void Awake() {
_rb = GetComponent<Rigidbody2D>();
Rb = GetComponent<Rigidbody2D>();
_playerInput = new PlayerInput();
@ -47,7 +50,7 @@ public class PlayerMovement1 : MonoBehaviour {
void Jump(InputAction.CallbackContext obj) {
if (!_grounded) return;
_rb.velocity = new Vector2(_rb.velocity.x, jumpForce);
Rb.velocity = new Vector2(Rb.velocity.x, jumpForce);
animator.SetTrigger(Jump1);
}
@ -73,13 +76,13 @@ public class PlayerMovement1 : MonoBehaviour {
// Update is called once per frame
void Update() {
animator.SetFloat(XVelocity, Mathf.Abs(_horizontalInput * speed));
animator.SetFloat(YVelocity, _rb.velocity.y);
animator.SetFloat(YVelocity, Rb.velocity.y);
animator.SetBool(Grounded, _grounded);
}
void FixedUpdate() {
_grounded = Physics2D.OverlapBox(transform.position, feetSize, 0, groundMask);
_rb.velocity = new Vector2(_horizontalInput * speed, _rb.velocity.y);
Rb.velocity = new Vector2(_horizontalInput * speed, Rb.velocity.y);
}
void OnDrawGizmos() {