Search Unity

Question Rotate Weapon child object of Player parent when facing left and perfoming an attack.

Discussion in 'Scripting' started by domonkosbarna, Feb 21, 2023.

  1. domonkosbarna

    domonkosbarna

    Joined:
    Jan 24, 2017
    Posts:
    1
    Hi everyone! Im struggling for a while with rotating the Weapon child object of Player parent when facing left and perfoming an attack in a 2D game. I tried many options now so i dont want to copy all of them. I tried setting weapon.transform.localScale, multiply the transform.position.x of the weapon (which is by the way what i would like to get). Nothing works. It must be a very simple basic function in every game - keeping the direction of child objects of the player, so there must be an easy way. I copy the PlayerMovement, PlayerAttack and weapon script. I tried in all of them. What my feelings suggest it would be the best done in the PlayerMovement script where i already check the direction of the player for the movement animation. Thank you for the help in advance.


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

    public class PlayerMovement : MonoBehaviour
    {
    public float moveSpeed = 5f;
    public float jumpForce = 10f;
    private SpriteRenderer spriteRenderer;
    private Animator animator;
    private Rigidbody2D rb;
    public GameObject weapon;
    private bool isRunning;
    public bool isOnGround = true;
    public bool isFacingLeft = false;

    void Start()
    {
    animator = GetComponent<Animator>();
    spriteRenderer = GetComponent<SpriteRenderer>();
    rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
    // Get the horizontal input
    float horizontal = Input.GetAxis("Horizontal");

    // Move the player horizontally
    Vector3 movement = new Vector3(horizontal, 0f, 0f) * moveSpeed * Time.deltaTime;
    transform.position += movement;

    // Set the "IsRunning" parameter based on the player's movement
    isRunning = (horizontal != 0f);
    animator.SetBool("isRunning", isRunning);

    // Flip the player's sprite horizontally if moving left
    if (horizontal < 0f)
    {
    spriteRenderer.flipX = true;
    isFacingLeft = true;
    }
    else if (horizontal > 0f)
    {
    spriteRenderer.flipX = false;
    isFacingLeft = false;
    }


    // Jump when the space button is pressed
    if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
    {
    rb.AddForce(Vector3.up * jumpForce, ForceMode2D.Impulse);
    isOnGround = false;
    }

    }

    // Set a condition for only be able to jump when he is on the ground
    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.collider.tag == "Ground")
    isOnGround = true;
    }
    }

    ******************************************************************************************

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

    public class PlayerAttack : MonoBehaviour
    {
    private Animator animator;
    public GameObject weapon;
    public float attackDelay = 1f;


    void Start()
    {
    animator = GetComponent<Animator>();
    weapon.SetActive(false);
    }


    void Update()
    {
    // Attack when the left mouse button is pressed
    if (Input.GetMouseButtonDown(0))
    {
    animator.SetTrigger("isAttacking");

    weapon.SetActive(true);
    StartCoroutine(DisableWeapon());

    }
    }

    private IEnumerator DisableWeapon()
    {
    yield return new WaitForSeconds(attackDelay);
    weapon.SetActive(false);
    }

    }

    ******************************************************************************************

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

    public class Weapon : MonoBehaviour
    {
    //public LayerMask enemyLayer;
    public int damage = 1;

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.layer == LayerMask.NameToLayer("Enemy"))
    {
    Debug.Log("Enemy collision detected");


    // Check if the collision has a valid gameObject
    if (collision != null)
    {
    // Get the TargetableObject component
    Targetable target = collision.gameObject.GetComponent<Targetable>();

    target.ReceiveDamage(damage);
    }
    }
    }
    }