using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class knockback : MonoBehaviour
{
public float thrust;
public float knockTime;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("breakable"))
{
other.GetComponent<pot>().smash();
}
if (other.gameObject.CompareTag("enemy") || other.gameObject.CompareTag("Player"))
{
Rigidbody2D hit = other.GetComponent<Rigidbody2D>();
if(hit != null)
{
Vector2 difference = hit.transform.position - transform.position;
difference = difference.normalized * thrust;
hit.AddForce(difference , ForceMode2D.Impulse);
if(other.gameObject.GetComponent("enemy"))
{
hit.GetComponent<enemy>().currentState = enemyState.stagger;
other.GetComponent<enemy>().Knock(hit, knockTime);
}
if(other.gameObject.GetComponent("Player"))
{
hit.GetComponent<PlayerMovement>().currentState = PlayerState.stagger;
other.GetComponent<PlayerMovement>().Knock(knockTime);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerState
{
walk,
attack,
interact,
stagger,
idle
}
public class PlayerMovement : MonoBehaviour
{
public PlayerState currentState;
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
void Start()
{
currentState = PlayerState.walk;
animator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
animator.SetFloat("moveX", 0);
animator.SetFloat("moveY", -1);
}
void Update()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if (Input.GetButtonDown("attack") && currentState != PlayerState.attack && currentState != PlayerState.stagger)
{
StartCoroutine(AttackCo());
}
else if(currentState == PlayerState.walk || currentState == PlayerState.idle)
{
UpdateAnimationAndMove();
}
}
private IEnumerator AttackCo()
{
animator.SetBool("attacking", true);
currentState = PlayerState.attack;
yield return null;
animator.SetBool("attacking", false);
yield return new WaitForSeconds(.3f);
currentState = PlayerState.walk;
}
void UpdateAnimationAndMove()
{
if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat("moveX", change.x);
animator.SetFloat("moveY", change.y);
animator.SetBool("moving", true);
}
else
{
animator.SetBool("moving", false);
}
}
void MoveCharacter()
{
change.Normalize();
myRigidbody.MovePosition(
transform.position + change * speed * Time.deltaTime
);
}
public void Knock(float knockTime)
{
StartCoroutine(KnockCo(knockTime));
}
private IEnumerator KnockCo(float knockTime)
{
if(myRigidbody != null)
{
yield return new WaitForSeconds(knockTime);
myRigidbody.velocity = Vector2.zero;
currentState = PlayerState.idle;
myRigidbody.velocity = Vector2.zero;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum enemyState
{
idle,
walk,
attack,
stagger
}
public class enemy : MonoBehaviour
{
public enemyState currentState;
public int health;
public string enemyName;
public int baseAttack;
public float moveSpeed;
public void Knock(Rigidbody2D myRigidBody, float knockTime)
{
StartCoroutine(KnockCo(myRigidBody, knockTime));
}
private IEnumerator KnockCo(Rigidbody2D myRigidBody, float knockTime)
{
if(myRigidBody != null)
{
yield return new WaitForSeconds(knockTime);
myRigidBody.velocity = Vector2.zero;
currentState = enemyState.idle;
myRigidBody.velocity = Vector2.zero;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class log : enemy
{
private Rigidbody2D myRigidbody;
public Transform target;
public float chaseRadius;
public float attackRadius;
public Transform homePosition;
void Start()
{
currentState = enemyState.idle;
myRigidbody = GetComponent<Rigidbody2D>();
target = GameObject.FindWithTag("Player").transform;
}
void FixedUpdate()
{
CheckDistance();
}
void CheckDistance()
{
if(Vector3.Distance(target.position, transform.position ) <= chaseRadius && Vector3.Distance(target.position , transform.position) > attackRadius)
{
if(currentState == enemyState.idle || currentState == enemyState.walk && currentState != enemyState.stagger)
{
Vector3 temp = Vector3.MoveTowards(transform.position, target.position ,moveSpeed * Time.deltaTime);
myRigidbody.MovePosition(temp);
ChangeState(enemyState.walk);
}
}
}
private void ChangeState(enemyState newState)
{
if(currentState != newState)
{
currentState = newState;
}
}
}
hello i’m trying to make a rpg game by learning from a channel on youtube and i’m new to this. There is a problem with the game I made. In the game, my character and the enemy start pushing each other when they touch, please help. And I’m sorry for my bad english.
video link :
and I added scripts
6901034–807827–enemy.cs (850 Bytes)
6901034–807830–knockback.cs (1.35 KB)
6901034–807833–log.cs (1.25 KB)
6901034–807836–PlayerMovement.cs (2.39 KB)