yuval gabay
yuval gabay

Reputation: 57

unity : rotating a gun 360 dgrees around the player in a 2d platfrom shooter

trying for 2 days to rotate a gun around a player in a 2d platfromer and i have 3 problems 1: the item or rotate uncontrolebly (my grammer isnt the best my mother language isnt even latin base) around the player even if i dont move my mouse .or its rotate on its self like a wheel. 2:its seems phisycs some how work on the gun even though its dosnt has a rigibid body. givin in rb on kinimatic halped but not fully. 3: its rotate way to quickly . little movment will cause it to fly 4(bonus) the sqauer module i gave it strach and band in a really weird way.

heres the code:your text

[SerializeField] float fireRate = 0.3f;
    [SerializeField] float rocketSpeed = 20f;
    [SerializeField] GameObject rocketType;
    [SerializeField] Transform firePoint;
    Player player;

    Vector3 mousePos;
    Camera mainCam;

    void Start()
    {
        player = FindObjectOfType<Player>();
        mainCam = FindObjectOfType<Camera>().GetComponent<Camera>();
    }

    // Update is called once per frame
    void Update()
    {
        mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
    }
    private void FixedUpdate()
    {
        Vector3 aimDirection = mousePos - transform.position;

        float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg ;

        // Quaternion rotation = Quaternion.AngleAxis(aimAngle, Vector3.forward);
        //  transform.rotation = rotation;
        transform.rotation = Quaternion.Euler(0, 0, aimAngle);
        // transform.RotateAround(player.transform.position, Vector3.forward, aimAngle);
       


}

here a picture of the componets: enter image description here

wanted a gun obj to rotate around my charcter whan its move or still, wanted to shoot stuff while i jump.

Upvotes: 0

Views: 448

Answers (1)

ITR
ITR

Reputation: 176

Mathf.Atan2 returns the value in Radians, but Quaternion.Euler uses Degrees. You'll have to multiply the value by Mathf.Rad2Deg first.

It also sounds like the parent of the gun has a rigidbody which is causing it to Rotate. Remove the rigidbody you put on the gun and toggle on "Freeze Rotation" on the parent's rigidbody.

Upvotes: 0

Related Questions