Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Area of attack getting off player

Discussion in '2D' started by bylduda, Jun 30, 2022.

  1. bylduda

    bylduda

    Joined:
    Apr 13, 2022
    Posts:
    4
    I have implemented script that rotate player depending on mouse direction (rotating).
    But gameobject, attack area, which is abstraction of attack doesn't stick to player.

    The most important thing is the way I acces child component:
    Code (CSharp):
    1.  attackArea = transform.GetChild(0).gameObject;
    2. attackArea.transform.rotation = rotationCalc;
    There are 3 scripts:
    Code (CSharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.  
    5.  
    6.     [SerializeField] private float speed = 3f;
    7.     public Vector3 positionMouse;
    8.     private Rigidbody2D body;
    9.     private Vector2 axisMovement;
    10.  
    11.         private GameObject attackArea = default;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         body = GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         axisMovement.x = Input.GetAxisRaw("Horizontal");
    24.         axisMovement.y = Input.GetAxisRaw("Vertical");
    25.  
    26.  
    27.         Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
    28.  
    29.         Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
    30.  
    31.         float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
    32.  
    33.         var rotationCalc =  Quaternion.Euler(new Vector3(0f, 0f, angle));
    34.        
    35.         transform.rotation = rotationCalc;
    36.  
    37.         attackArea = transform.GetChild(0).gameObject;
    38.  
    39.         attackArea.transform.rotation = rotationCalc;
    40.      
    41.     }
    42.  
    43.     private float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
    44.     {
    45.         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
    46.     }
    47.     private void FixedUpdate()
    48.     {
    49.         Move();
    50.     }
    51.  
    52.     private void Move()
    53.     {
    54.         body.velocity = axisMovement.normalized * speed;
    55.     }
    56. }
    57.  
    second one:
    Code (CSharp):
    1. public class PlayerAttack : MonoBehaviour
    2. {
    3.     private GameObject attackArea = default;
    4.  
    5.     private bool attacking = false;
    6.  
    7.     private float timeToAttack = 0.25f;
    8.  
    9.     private float timer = 0f;
    10.  
    11.       private Rigidbody2D body;
    12.  
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         attackArea = transform.GetChild(0).gameObject;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.      
    24.  
    25.         if (Input.GetKeyDown(KeyCode.Space))
    26.         {
    27.             Debug.Log("SPACE DOWN");
    28.             Attack();
    29.         }
    30.  
    31.         if (attacking)
    32.         {
    33.             timer += Time.deltaTime;
    34.  
    35.             if (timer >= timeToAttack)
    36.             {
    37.                 timer = 0;
    38.                 attacking = false;
    39.                 attackArea.SetActive(attacking);
    40.             }
    41.  
    42.  
    43.         }
    44.     }
    45.  
    46.     private void Attack()
    47.     {
    48.         attacking = true;
    49.         attackArea.SetActive(attacking);
    50.     }
    51. }
    52.  
    last one, attack area:
    Code (CSharp):
    1.  
    2. public class AttackArea : MonoBehaviour
    3. {
    4.     [SerializeField] private int damage = 3;
    5.  
    6.    private void OnTriggerEnter2D(Collider2D collider)
    7.     {
    8.         if(collider.GetComponent<Health>() != null)
    9.         {
    10.             Health health = collider.GetComponent<Health>();
    11.             health.Damage(damage);
    12.         }
    13.     }
    14. }
    How it should look:
    upload_2022-6-30_19-17-53.png - always sticked,

    but it gets off and looks like this when game is on:
    upload_2022-6-30_19-18-31.png
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    There are a few things i would do to test this. First, make sure the attack area gameobject is a child of the moving square. Then, mess around with the attackArea variable where you are setting its transform rotation.

    My suggestion, is let the parent/child portion do it all. If you have the attackArea gameobject as a child of the movement square, it should stay exactly where you place it but move and rotate with the square. You shouldnt have to do any code to tell it to stay there. Try commenting out all of the attackArea.transform stuff and the enabling/disabling of it.
     
    bylduda likes this.
  3. bylduda

    bylduda

    Joined:
    Apr 13, 2022
    Posts:
    4
    Exactly as I though at first time, like it stick do object, so what can go wrong.
    ....SO
    Finally I think its resolved, I had simulated opiton checked in rigidbody2d on area attack, now its working.
    Btw Rigidbody has to be attached here, like its a solution from my previous post on forum.


    Thank you very much!

    @Edit I was wrong.
    The solution was change body type to kinematic (attack area) from dynamic. Simulated its still on.
     
    Last edited: Jun 30, 2022
    Cornysam likes this.