From 40dda1c64b81f290fe5a4d7a709c8cb604f4990f Mon Sep 17 00:00:00 2001 From: Anonymous Raccoon <32224410+AnonymusRaccoon@users.noreply.github.com> Date: Sun, 11 Mar 2018 20:40:05 +0100 Subject: [PATCH] Creating Dash. --- .../Prefabs/Network Prefabs/GamePlayer.prefab | 15 +++-- .../Network Prefabs/LobbyPlayer.prefab | 3 + Assets/Script/PlayerMovement.cs | 67 ++++++++++++++++--- Assets/Script/SmallProjectile.cs | 23 +++++++ Assets/Script/SmallProjectile.cs.meta | 13 ++++ 5 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 Assets/Script/SmallProjectile.cs create mode 100644 Assets/Script/SmallProjectile.cs.meta diff --git a/Assets/Prefabs/Network Prefabs/GamePlayer.prefab b/Assets/Prefabs/Network Prefabs/GamePlayer.prefab index e87b1ae..7f00667 100644 --- a/Assets/Prefabs/Network Prefabs/GamePlayer.prefab +++ b/Assets/Prefabs/Network Prefabs/GamePlayer.prefab @@ -113,7 +113,11 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Horizontal: + Vertical: JumpKey: 0 + HookKey: 0 + DashKey: 0 + UltKey: 0 speed: 10 airSpeed: 7 jumpForce: 8 @@ -121,17 +125,16 @@ MonoBehaviour: gravity: -10 wallJump: 4 wallJumpPush: 10 - wallJumpSpeed: 0 + smallJumpPush: 5 pushSpeed: 0.2 - wallMask: - serializedVersion: 2 - m_Bits: 512 + dashSpeed: 15 + smallProjectile: {fileID: 1556668439340832, guid: be1300b1777602f448d537a75a9093c3, + type: 2} + sProjSpeed: 2 playerMask: serializedVersion: 2 m_Bits: 1024 setuped: 0 - misSliding: 0 - wallDirection: 0 --- !u!114 &114186454880906932 MonoBehaviour: m_ObjectHideFlags: 1 diff --git a/Assets/Prefabs/Network Prefabs/LobbyPlayer.prefab b/Assets/Prefabs/Network Prefabs/LobbyPlayer.prefab index 7f47428..5c18d82 100644 --- a/Assets/Prefabs/Network Prefabs/LobbyPlayer.prefab +++ b/Assets/Prefabs/Network Prefabs/LobbyPlayer.prefab @@ -129,6 +129,9 @@ MonoBehaviour: smallJumpPush: 5 pushSpeed: 0.2 dashSpeed: 15 + smallProjectile: {fileID: 1556668439340832, guid: be1300b1777602f448d537a75a9093c3, + type: 2} + sProjSpeed: 2 playerMask: serializedVersion: 2 m_Bits: 1024 diff --git a/Assets/Script/PlayerMovement.cs b/Assets/Script/PlayerMovement.cs index ef8d18a..7fc5264 100644 --- a/Assets/Script/PlayerMovement.cs +++ b/Assets/Script/PlayerMovement.cs @@ -22,10 +22,14 @@ public class PlayerMovement : MonoBehaviour [SerializeField] private int wallJumpPush = 10; [SerializeField] private int smallJumpPush = 5; [SerializeField] private float pushSpeed = 0.2f; - [SerializeField] private float dashSpeed = 15; [Space] + [SerializeField] private float dashSpeed = 15; [SerializeField] private GameObject smallProjectile; + [SerializeField] private float sProjSpeed = 2; + private bool canDash = true; + private float dashTime = 0; + private Vector3 dashVelocity; [Space] @@ -35,7 +39,7 @@ public class PlayerMovement : MonoBehaviour [Space] public bool setuped = false; - private float velocity = 0; + [HideInInspector] public float velocity = 0; private bool groundedLastFrame = false; private int wallDirection; @@ -186,6 +190,9 @@ public class PlayerMovement : MonoBehaviour if (movingPlateform != null && !isGrounded && !groundedLastFrame && !isSliding) movingPlateform = null; + //Reload dash + if (isGrounded || isSliding || isPushing) + canDash = true; if (-1 < velocity && velocity < 1) velocity = 0; @@ -205,8 +212,30 @@ public class PlayerMovement : MonoBehaviour horizontalVel /= 2; } + if(dashTime > 0) + { + rb.velocity = dashVelocity; + //dashVelocity.x -= dashVelocity.x / 10; + dashVelocity.y -= dashVelocity.y / 10; + dashTime--; + + if(dashTime == 0) + { + //Create projectile + GameObject proj = Instantiate(smallProjectile, rb.position + new Vector3(rb.velocity.x / 10, rb.velocity.y / 5, 0), Quaternion.identity); + proj.name = "SmallProjectile"; + proj.GetComponent().velocity = new Vector3(dashVelocity.x * sProjSpeed, dashVelocity.y * sProjSpeed, 0); + proj.GetComponent().sender = this; + + dashVelocity = Vector3.zero; + } + } + if(!isSliding || (isSliding && Mathf.Sign(wallDirection) != Mathf.Sign(Input.GetAxis(Horizontal)))) - rb.AddForce(new Vector3(horizontalVel * (isPushing ? pushSpeed : 1) * (airControl ? airSpeed : speed) - (rb.velocity.x - velocity), 0, 0), ForceMode.Impulse); + { + if (dashTime <= 0) + rb.AddForce(new Vector3(horizontalVel * (isPushing ? pushSpeed : 1) * (airControl ? airSpeed : speed) - (rb.velocity.x - velocity), 0, 0), ForceMode.Impulse); + } //Make user jump if (Input.GetKey(JumpKey) && isGrounded) @@ -253,18 +282,34 @@ public class PlayerMovement : MonoBehaviour } //Dash and small attack - if (Input.GetKeyDown(DashKey)) + if (canDash && Input.GetKeyDown(DashKey)) { - horizontalVel = Input.GetAxis(Horizontal); - float verticalVel = Input.GetAxis(Vertical); + Vector3 movement = NormaliseMovement(Input.GetAxis(Horizontal), Input.GetAxis(Vertical)); - //if(horizontalVel ) //if horizontal and vertical vel are null, set horizontal to 0.80 + dashTime = 8; + dashVelocity = new Vector3(dashSpeed * movement.x, dashSpeed * movement.y, 0); - rb.velocity = new Vector3(dashSpeed * horizontalVel, dashSpeed * verticalVel, 0); - velocity = dashSpeed * Input.GetAxis(Horizontal); - //Instantiate(smallProjectile, rb.position) + rb.velocity = new Vector3(dashSpeed * movement.x, dashSpeed * movement.y, 0); + velocity = dashSpeed * movement.x; + jumpDirection = movement.x; + canDash = false; } - + } + + Vector3 NormaliseMovement(float x, float y) + { + Vector3 input = new Vector3(x, y, 0); + input.Normalize(); + + if (input.x == 0 && input.x == 0) + input.x = 1; + + return input; + } + + public void SmallProjectileHit(PlayerMovement victim) + { + canDash = true; } private void OnTriggerEnter(Collider other) diff --git a/Assets/Script/SmallProjectile.cs b/Assets/Script/SmallProjectile.cs new file mode 100644 index 0000000..a08b35a --- /dev/null +++ b/Assets/Script/SmallProjectile.cs @@ -0,0 +1,23 @@ +using UnityEngine; + +public class SmallProjectile : MonoBehaviour +{ + [HideInInspector] public PlayerMovement sender; + [SerializeField] private float pushForce = 1; + + private void OnCollisionEnter(Collision collision) + { + if(collision.gameObject.tag == "Player") + { + Rigidbody rb = GetComponent(); + PlayerMovement pMovement = collision.gameObject.GetComponent(); + + pMovement.velocity = rb.velocity.x * pushForce; + collision.gameObject.GetComponent().velocity = rb.velocity * pushForce; + + sender.SmallProjectileHit(pMovement); + } + + Destroy(gameObject); + } +} diff --git a/Assets/Script/SmallProjectile.cs.meta b/Assets/Script/SmallProjectile.cs.meta new file mode 100644 index 0000000..2738236 --- /dev/null +++ b/Assets/Script/SmallProjectile.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: d455af6e3ed71b546b353798c6dcd1b6 +timeCreated: 1520796005 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: