Why won't my Skeleton deal damage to my player? - c#

So, every time the skeleton hit the player/character. It won't show the word "HIT!". What did I do wrong?
THE PLAYER IMAGE INSPECTOR
THE SKELETON IMAGE INSPECTOR
THE HIERARCHY IMAGE
SKELETON ENEMY SCRIPT
private Rigidbody2D myBody;
[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;
private bool patrol, detect;
private Transform playerPos;
private Animator anim;
[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;
//sound
VOID AWAKE
void Awake()
{
anim = GetComponent<Animator>();
playerPos = GameObject.Find("George").transform;
myBody = GetComponent<Rigidbody2D>();
}
VOID START
private void Start()
{
maxX = transform.position.x + (distance);
minX = maxX - distance;
//if (Random.value > 0.5) direction = 1;
//else direction = -1;
}
VOID UPDATE
void Update()
{
if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
else patrol = true;
}
VOID FIXED UPDATE
private void FixedUpdate()
{
if (anim.GetBool("Death"))
{
myBody.velocity = Vector2.zero;
GetComponent<Collider2D>().enabled = false;
myBody.isKinematic = true;
anim.SetBool("Attack", false);
return;
}
if (myBody.velocity.x > 0)
{
transform.localScale = new Vector2(1f, transform.localScale.y);
anim.SetBool("Attack", false);
}
else if
(myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);
if (patrol)
{
detect = false;
switch (direction)
{
case -1:
if (transform.position.x > minX)
myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
else
direction = 1;
break;
case 1:
if (transform.position.x < maxX)
myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
else
direction = -1;
break;
}
}
else
{
if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
{
if (!detect)
{
detect = true;
anim.SetTrigger("Detect");
myBody.velocity = new Vector2(0, myBody.velocity.y);
}
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;
Vector3 playerDir = (playerPos.position - transform.position).normalized;
if (playerDir.x > 0)
myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
else
myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
}
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
anim.SetBool("Attack", true);
}
}
}
VOID ATTACK
public void Attack()
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
Collider2D attackPlayer = Physics2D.OverlapCircle(attackPos.position, attackRange, playerLayer);
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
PLAYER HEALTH SCRIPT
VOID AWAKE
public int health = 100;
void Awake()
{
}
VOID UPDATE
void Update()
{
if (health < 1)
{
print("Dead");
}
}
VOID TAKE DAMAGE
public void TakeDamage(int damage)
{
FindObjectOfType<CameraShake>().ShakeItMedium();
health -= damage;
}
private void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Fireball")
{
TakeDamage(25);
}
}

You dont seem to call the "Attack" method in your script.
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
// calling the attack method, so the physics cast is being made
Attack();
anim.SetBool("Attack", true);
}
And dont forget to change this, you are checking if the cast hit nothing instead of something.
// if the cast hit something
if (attackPlayer != null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}

While there maybe many other things wrong with that long pile of code:
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
You check if attackPlayer is null and then try do work on it... Im pretty you sure you meant if (attackPlayer != null)...

Related

Camera does not contain a definition for ViewportPointToRay

I'm trying to make a script that can shoot stuff but this line isn't working. I've googled the issue and even unity says that this is correct
using UnityEngine;
using TMPro;
public class ShootGun_Script : MonoBehaviour
{
public GameObject bullet;
public float shootForce, upwardForce;
public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
public Rigidbody playerRb;
public float recoilForce;
bool shooting, readyToShoot, reloading;
public Camera fpsCam;
public Transform attackPoint;
public GameObject muzzleFlash;
public TextMeshProUGUI ammunitionDisplay;
public bool allowInvoke = true;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
if (ammunitionDisplay != null)
ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
}
private void MyInput()
{
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
bulletsShot = 0;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
** Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));**
RaycastHit hit;
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75);
Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0);
GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);
currentBullet.transform.forward = directionWithSpread.normalized;
currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
if (muzzleFlash != null)
Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot++;
if (allowInvoke)
{
Invoke("ResetShot", timeBetweenShooting);
allowInvoke = false;
playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
}
if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
readyToShoot = true;
allowInvoke = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}

Coding smoother Shooting Recoil

So, I started following a tutorial and then added some other things that I needed and everything works fine, even the recoil, but the problem is that it is really choppy, it moves once a frame and it isn't smooth at all (which is what I want) I don't know a lot about programming so I hope you can help me :)
My code:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class GunController : MonoBehaviour
{
[Header("Gun Setting")]
public float fireRate = 0.1f;
public int clipSize = 30;
public int reservedAmmoCapacity = 270;
//Variables that change throughout the code
bool canShoot;
int _currentAmmoInClip;
int _ammoInReserve;
//Muzzle Flash
public ParticleSystem muzzleFlash;
//Aiming
public Vector3 normalLocalPosition;
public Vector3 aimingLocalPosition;
public float aimSmoothing = 10;
[Header("Mouse Settings")]
public float mouseSensitivity = 1;
Vector2 _currentRotation;
public float weaponSwayAmount = 10;
//Weapon Recoil
public bool randomizeRecoil;
public Vector2 randomRecoilConstraints;
//You only need to assign if randomize recoil is off
public Vector2[] recoilPattern;
//Audio
AudioSource shootingSound;
//Reloading
public float reloadTime = 1.5f;
private void Start()
{
_currentAmmoInClip = clipSize;
_ammoInReserve = reservedAmmoCapacity;
canShoot = true;
shootingSound = GetComponent<AudioSource>();
}
private void Update()
{
DetermineAim();
DetermineRotation();
if (Input.GetMouseButton(0) && canShoot && _currentAmmoInClip > 0)
{
shootingSound.Play();
StartCoroutine(FinishShooting());
muzzleFlash.Play();
canShoot = false;
_currentAmmoInClip--;
StartCoroutine(ShootGun());
}
else if (Input.GetKeyDown(KeyCode.R) && _currentAmmoInClip < clipSize && _ammoInReserve > 0)
{
StartCoroutine(Reload());
}
}
void DetermineRotation()
{
Vector2 mouseAxis = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseAxis *= mouseSensitivity;
_currentRotation += mouseAxis;
_currentRotation.y = Mathf.Clamp(_currentRotation.y, -90, 90);
transform.localPosition += (Vector3)mouseAxis * weaponSwayAmount / 1000;
transform.root.localRotation = Quaternion.AngleAxis(_currentRotation.x, Vector3.up);
transform.parent.localRotation = Quaternion.AngleAxis(-_currentRotation.y, Vector3.right);
}
void DetermineAim()
{
Vector3 target = normalLocalPosition;
if (Input.GetMouseButton(1)) target = aimingLocalPosition;
Vector3 desiredPosition = Vector3.Lerp(transform.localPosition, target, Time.deltaTime * aimSmoothing);
transform.localPosition = desiredPosition;
}
void DetermineRecoil()
{
transform.localPosition -= Vector3.forward * 0.1f;
if (randomizeRecoil)
{
float xRecoil = Random.Range(-randomRecoilConstraints.x, randomRecoilConstraints.x);
float yRecoil = Random.Range(-randomRecoilConstraints.y, randomRecoilConstraints.y);
Vector2 recoil = new Vector2(xRecoil, yRecoil);
_currentRotation += recoil;
}
else
{
int currentStep = clipSize + 1 - _currentAmmoInClip;
currentStep = Mathf.Clamp(currentStep, 0, recoilPattern.Length - 1);
_currentRotation += recoilPattern[currentStep];
}
}
IEnumerator ShootGun()
{
_currentAmmoInClip -= 1;
DetermineRecoil();
RayCastEnemy();
yield return new WaitForSeconds(fireRate);
canShoot = true;
}
void RayCastEnemy()
{
RaycastHit hit;
if (Physics.Raycast(transform.parent.position, transform.parent.forward, out hit, 1 << LayerMask.NameToLayer("Enemy")))
{
try
{
Debug.Log("Hit an enemy");
Rigidbody rb = hit.transform.GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.None;
rb.AddForce(transform.parent.transform.forward * 50);
}
catch { }
}
}
IEnumerator FinishShooting()
{
yield return new WaitForSeconds(0.3f);
shootingSound.Stop();
}
IEnumerator Reload()
{
canShoot = false;
yield return new WaitForSeconds(reloadTime);
int amountNeeded = clipSize - _currentAmmoInClip;
if (amountNeeded > _ammoInReserve)
{
_currentAmmoInClip += _ammoInReserve;
_ammoInReserve -= amountNeeded;
canShoot = true;
}
else
{
canShoot = false;
_currentAmmoInClip = clipSize;
_ammoInReserve -= amountNeeded;
canShoot = true;
}
}
}
I don't see the function DetermineRecoil() called anywhere in the code, but maybe this solution will work:
let _currentRotation = Vector2.Lerp(_currentRotation, _currentRotation + recoilPattern[currentStep], 10*Time.deltaTime);
It will change the value of _currentRotation smoothly.

How can I fix my dash not working properly when jumping and how can I add a cooldown?

Recently I have been Frankensteining code I've found online together and I am struggling to implement the dash correctly. In its current state, whenever my character jumps and then uses his dash, he gets caught in the air as if the dash didn't move him. Obviously this inst intended and I was wondering how I could fix this. Also, how could I include a cool down system to my dash ability. I've tried using various sources for cool down scripts but they always seem to have a downside.
Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
//Player Movement
public float speed;
public float jumpForce;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float dashSpeed;
public float startDashTime;
private float timeStamp = 0;
public Animator animator;
private Rigidbody2D rb;
private float moveInput;
private bool isGrounded;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private float dashTime;
public int direction;
void Start()
{
animator.GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
if (direction < 1)
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update()
{
// Moving
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
animator.SetBool("Moving", true);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
animator.SetBool("Moving", true);
}
else
{
animator.SetBool("Moving", false);
}
// Jumping
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("IsJumping");
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if (isGrounded == false)
{
animator.SetBool("Grounded", false);
}
if (isGrounded == true)
{
animator.SetBool("Grounded", true);
}
// Dashing
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if ((transform.rotation.eulerAngles.y == 180))
{
Dashleft();
}
else
{
DashRight();
}
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
}
}
void Dashleft()
{
direction = 1;
rb.velocity = Vector2.left * dashSpeed;
}
void DashRight()
{
direction = 1;
rb.velocity = Vector2.right * dashSpeed;
}
}
}

Why does my script make my "dash" have a seemingly random duration and how can I fix it?

I am new to coding. I've been trying to Frankenstein basic tutorials into something of my own and it seems ive finally hit a wall. I'm really not sure what is causing this and would like some help to weed out the problem.
at the moment when I press the dash button (left shift) my character dashes in the direction he is facing but for a random duration. I would like it to be consistent and working properly.
Thanks in advance!
PS: Sorry for the long script, it contains everything to do with player movment.
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
//Player Movement
public float speed;
public float jumpForce;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float dashSpeed;
public float startDashTime;
public float dashCooldownTime = 2;
private float nextFireTime = 0;
public Animator animator;
private Rigidbody2D rb;
private float moveInput;
private bool isGrounded;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private float dashTime;
public int direction;
void Start()
{
animator.GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
if (direction < 1 )
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update()
{
// Moving
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
animator.SetBool("Moving", true);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
animator.SetBool("Moving", true);
}
else
{
animator.SetBool("Moving", false);
}
// Jumping
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("IsJumping");
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if (isGrounded == false)
{
animator.SetBool("Grounded", false);
}
if (isGrounded == true)
{
animator.SetBool("Grounded", true);
}
// Dashing
if (Time.time > nextFireTime)
{
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
UnityEngine.Debug.Log("beaners");
nextFireTime = Time.time + dashCooldownTime;
if ((transform.rotation.eulerAngles.y == 180))
{
Dashleft();
}
else
{
DashRight();
}
}
}
}
else
{
if(dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
}
else
{
dashTime -= Time.deltaTime;
}
}
}
void Dashleft()
{
direction = 1;
rb.velocity = Vector2.left * dashSpeed;
}
void DashRight()
{
direction = 1;
rb.velocity = Vector2.right * dashSpeed;
}
}
Random duration is because of the use of Rigidbody2D.
See in Unity Rigidbody2D is responsible for each and every single physical interaction. It mean forces from player like player movement and forces from your environment like friction and collision.
Here what is happening is that when your player is dashing there is also player movement force, which is also applied on your player's Rigidbody2d. And friction of ground on which your player is running/dashing on.
I've added a comment on parts that I've changed.
Updated part of your script:
private bool isDashing;
.
.
.
void Update()
{
//Added: putting a Condition to check if you player is not dashing, if it is then
//player won't be able to do anything, As I've scene and done in many games. if you
//don't want this remove it and see what happens, this is not tested so sorry if there
//is something that is not working as intended.
//dashing condition check
if(!isDashing)
{
// Moving
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
animator.SetBool("Moving", true);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
animator.SetBool("Moving", true);
}
else
{
animator.SetBool("Moving", false);
}
// Jumping
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("IsJumping");
isJumping = true;
jumpTimeCounter = jumpTime;
//Changed: use Add force method instead of changing the velocity.
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
//rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
//Changed: Same this rb.AddForce...
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
//rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if (isGrounded == false)
{
animator.SetBool("Grounded", false);
}
if (isGrounded == true)
{
animator.SetBool("Grounded", true);
}
}
//dashing condition ends
// Dashing
if (Time.time > nextFireTime)
{
//Here your Dash timer ends.
isDashing = false;
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
UnityEngine.Debug.Log("beaners");
nextFireTime = Time.time + dashCooldownTime;
//Here your Dash timer Starts.
isDashing = true;
if ((transform.rotation.eulerAngles.y == 180))
{
Dashleft();
}
else
{
DashRight();
}
}
}
}
else
{
if(dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
}
else
{
dashTime -= Time.deltaTime;
}
}
}
visit Unity - Scripting API: Rigidbody2D.AddForce hope it helps.
Happy Coding

How do I fix my 2D enemy character from sliding instead of stopping when it gives a warning when a character is near to it's range

In game overview
I am still new to C#, and I wanted to know how to fix this problem. The enemy skeleton seems to slide when it reaches the range of the character. I tried finding the solution but failed to do so.
------------------------------ENEMY SCRIPT--------------------------------
private Rigidbody2D myBody;
[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;
private bool patrol, detect;
private Transform playerPos;
private Animator anim;
[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;
AWAKE
void Awake()
{
anim = GetComponent<Animator>();
playerPos = GameObject.Find("George").transform;
myBody = GetComponent<Rigidbody2D>();
}
VOID START
private void Start()
{
maxX = transform.position.x + (distance);
minX = maxX - distance;
//if (Random.value > 0.5) direction = 1;
//else direction = -1;
}
UPDATE
void Update()
{
if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
else patrol = true;
}
FIXED UPDATE
private void FixedUpdate()
{
if (anim.GetBool("Death"))
{
myBody.velocity = Vector2.zero;
GetComponent<Collider2D>().enabled = false;
myBody.isKinematic = true;
anim.SetBool("Attack", false);
return;
}
if (myBody.velocity.x > 0)
{
transform.localScale = new Vector2(1f, transform.localScale.y);
anim.SetBool("Attack", false);
}
else if
(myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);
if (patrol)
{
detect = false;
switch (direction)
{
case -1:
if (transform.position.x > minX)
myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
else
direction = 1;
break;
case 1:
if (transform.position.x < maxX)
myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
else
direction = -1;
break;
}
}
else
{
if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
{
if (!detect)
{
detect = true;
anim.SetTrigger("Detect");
myBody.velocity = new Vector2(0, myBody.velocity.y);
}
if (anim.GetCurrentAnimatorStateInfo(0).IsName("detect")) return;
Vector3 playerDir = (playerPos.position - transform.position).normalized;
if (playerDir.x > 0)
myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
else
myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
}
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0f)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
anim.SetBool("Attack", true);
}
}
}

Categories

Resources