Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { float runSpeed = 6f; float jumpSpeed = 14f; float gravityScale; bool

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour { float runSpeed = 6f; float jumpSpeed = 14f;

float gravityScale;

bool isRunning = false; bool isAlive = true;

Rigidbody2D rb; Animator anim;

[SerializeField] AudioClip jumpSound; [SerializeField] AudioClip deathSound;

void Start() { rb = GetComponent(); anim = GetComponent(); gravityScale = rb.gravityScale; }

void Update() { if (!isAlive) return;

Run(); FlipDirection(); Jump(); Die(); }

void Run() { float axisVal = Input.GetAxis("Horizontal");

Vector2 runVelocity = new Vector2(axisVal * runSpeed, rb.velocity.y); rb.velocity = runVelocity;

if (isRunning) { anim.SetBool("Run", true); }

else { anim.SetBool("Run", false); } }

void Jump() { if (!rb.IsTouchingLayers(LayerMask.GetMask("Ground"))) { anim.SetBool("Jump", false); return; }

if (Input.GetButtonDown("Jump")) { Vector2 jumpVelocity = new Vector2(0f, jumpSpeed); rb.velocity += jumpVelocity; anim.SetBool("Jump", true); AudioSource.PlayClipAtPoint(jumpSound, Camera.main.transform.position); }

}

void FlipDirection() { isRunning = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;

if (isRunning) { transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f); } }

public void LoadNextScene() { int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

SceneManager.LoadScene(currentSceneIndex + 1);

Debug.LogError(currentSceneIndex.ToString()); }

void Die() { if (rb.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards"))) { anim.SetTrigger("Die"); rb.velocity = new Vector2(1f, 1f); isAlive = false; StartCoroutine(PlayerDied()); AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position); } }

IEnumerator PlayerDied() { Time.timeScale = 1f; yield return new WaitForSecondsRealtime(1f); Time.timeScale = 1f; FindObjectOfType().ProcessPlayerDeath(); } }

I am trying to create a 2D Platformer and I have this current code based off of courses I've taken that shows how I'm controlling the player character. How do I add the ability to attack, like with a sword swipe, to this code. I'm not really sure how to go about it. I wanna try and make it so my character can attack and damage enemies in front of them whenever I press a button. I am using Unity.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Prelude To Programming

Authors: Stewart Venit, Elizabeth Drake

6th Edition

013374163X, 978-0133741636

More Books

Students also viewed these Accounting questions