Search Unity

How to implement weapon movement around the player using mouse pos?

Discussion in '2D' started by sotrosh, Jan 18, 2018.

  1. sotrosh

    sotrosh

    Joined:
    Dec 25, 2014
    Posts:
    13
    I need help with implementation weapon movement around the player with constant radius position. Current image describe how it should work:
    Thank you
     
  2. Brightori

    Brightori

    Joined:
    Sep 15, 2017
    Posts:
    64
    u need parent weapon to empty gameobject, and rotate empty gameobject


    Code (CSharp):
    1.  void Update() {
    2.      Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
    3.      float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    4.      transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    5. }
     
  3. sotrosh

    sotrosh

    Joined:
    Dec 25, 2014
    Posts:
    13
    Thank you for reply. This code rotates itself weaponObject around but I need to rotate weaponObject around player based on mouse position.
     
  4. Brightori

    Brightori

    Joined:
    Sep 15, 2017
    Posts:
    64
    as i say before - make weapon parent of emptyobj, and rotate this obj.