I’m working on my first game, a 2D top down in which I want to add a sword attack. I know I’m supposed to use a box collider or an Overlap object but I can’t get it to face the direction my character is looking. I’ve found tutorials on how to use the mous to aim but i’m using a controller with the new input system. I’ve already created an OnMelee binded to the button of my controller.
Does anyone know how I can make the attack follow my player ?
Where did you put your sword/object? Can’t you just parent it under the player so it rotates with the player?
I tried doing that but it did not rotate with my player
Can you make a screenshot? and how did you rotate the player, by setting the rotation or just changing a sprite?
my movement and input code looks like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private float _speed;
public Rigidbody2D _rigidbody;
private Vector2 _movementInput;
public Animator animator;
public SpriteRenderer spriteRenderer;
public static PlayerMovement instance;
public PlayerStamina playerStamina;
bool canMove = true;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("Plus d'une instance de PlayerMovement dans la scene");
return;
}
instance = this;
_rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
_rigidbody.velocity = _movementInput * _speed;
Flip(_rigidbody.velocity.x);
}
private void OnMove(InputValue inputValue)
{
_movementInput = inputValue.Get<Vector2>();
if (canMove == true)
{
if (_movementInput.x != 0 || _movementInput.y !=0){
animator.SetFloat("X", _movementInput.x);
animator.SetFloat("Y", _movementInput.y);
animator.SetBool("IsWalking", true);
} else {
animator.SetBool("IsWalking", false);
}
}
}
void Flip(float velocity)
{
if (_movementInput.x > 0.1f)
{
spriteRenderer.flipX = false;
}else if (_movementInput.x < -0.1f)
{
spriteRenderer.flipX = true;
}
}
void OnMelee()
{
if (playerStamina.isTired == false)
{
StartCoroutine(SwordAttack());
}
}
public IEnumerator SwordAttack()
{
animator.SetTrigger("MeleeAttack");
yield return new WaitForSeconds(0.5f);
playerStamina.TakeDamage(100);
}
Hm ok so your player can just look left and right? If you want to use the sword as a child element you can’t just use flipX on the sprite. You would need something like:
gameobject.transform.rotation = Quaternion.Euler(0f, 0f, 90f);
(0,0,90) for walking right and (0,0,-90) for walking right.
Then the children will rotate with it.
If you want to use flipX, you will also need to reposition the weapon. What comes to mind is you have two empty gameobjects for your player, one at the left side of the sprite, one at the right and with the correct rotation. And when you use flipX you also reposition your weapon to the gameobject on that side, parent it under it and set localPosition and localRotation to 0.
Maybe you can add a screenshot if I am understanding the setup incorrectly.
If you are using the first approach you can even rotate your character in any direction, including top/down and any angle you want by using this:
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);```
With Direction being the Vector3 you want to move in, left would be (-1,0,0) and top would be (0,-1,0) but getting your analog controller input you can even walk into directions like (0.78, 0.12,0)
My character can look in 4 directions, I had to do a flipX cause the spritesheet was lacking a facing left animation. works perfectly fine with the analog stick of my controller or my wasd
Oh, I see, all directions are handled by the animator and you have sprites for every direction. So rotating the gameobject is not working (I thought it was really total top-down like Hotline Miami).
I guess then you need four spots on your player where to put the sword depending on the movement direction.