Player attacks to the left in 2D game

hello everyone I have a question on a problem I have with my game development. I’m currently taking a unity course “Unity game Dev 2D” and I’m doing the second to the last tutorial of that series. I added some attack function and animation but when I attack the character will attack to the left even if I’m facing to the right. is there a way to fix this? Thanks

Hi,

Since we flip the player with the scale, the gun should get flipped, too. Have you already rewatched the video at least one more time? Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

If so, please share more information on what you did and see in Unity.

This is a different attack I’m trying to do, He does attack but when I do the attack he attacks to the left. the attack point always stays in front of him but just attacks to the left unless I hold down the right arrow key then attack.
here I will show the attack point in these screen shots.


AttackPoint2

If you don’t want to solve this problem with the scale or if the scale won’t solve your problem, you could define two attack points which you control via code. You just have to define which attack point is supposed to be active in which case.

1 Like

I did figure out that it is probably the scale on the X axis, I did put on a rigid body 2d on the attack point then added this script to the attack point. and I think I need to do a little more.
''using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackPoint : MonoBehaviour
{

Rigidbody2D attackPointRigidBody;
// Start is called before the first frame update
void Start()
{
    attackPointRigidBody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
     FlipAttackPoint();
}

void FlipAttackPoint()
{
    transform.localScale = new Vector2 (Mathf.Sign(attackPointRigidBody.velocity.x),1f);
}

}

Good job! :slight_smile:

still not solved any suggestions?

can I send my build to someone to look at?

I figured it out. Now I feel dumb :rofl: :crazy_face: I assigned my key press to the “A” key stroke. Well for you that don’t know. this key press will automatically make your character go left. And “D” will go Right and so on. :face_with_raised_eyebrow:

I’m glad you solved the problem yourself. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms