top down 2d game, aim weapon with mouse. please help
So i have tried some different solutions on this but every piece of code i have tried gives me the same result. im trying to make a 2d top down game where you can shoot. i have a weapon in front of my character which shoots. when my mouse is close to the character it seems accurate but the further i go the less accurate it becomes for some reason.


the blue line is just something i added to easier watch how unaccurate it is btw.
my code is
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     Rigidbody2D rbody2d;
     Animator anim;
     Vector2 mouseDir;
     Vector2 movement;
     Vector3 mousePos;
 
     public GameObject weaponSlot;
     public float moveSpeed = 5f;
 
 
     // Use this for initialization
     void Start () {
         rbody2d = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
         
 
         //player animation based on velocity
         if (movement != Vector2.zero) {
             anim.SetBool ("isWalk", true);
         } else {
             anim.SetBool ("isWalk", false);
         }
 
         //sets the animation direction based on mouse position
         if (mouseDir != Vector2.zero) {
             anim.SetFloat ("input_x", mouseDir.x);
             anim.SetFloat ("input_y", mouseDir.y);
         }
 
 
     }
 
     void FixedUpdate() {
 
 
 
         //gets the position of the mouse
         var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         Quaternion rot = Quaternion.LookRotation (weaponSlot.transform.position - mousePosition, Vector3.forward);
 
         weaponSlot.transform.rotation = rot;
         weaponSlot.transform.eulerAngles = new Vector3 (0, 0, weaponSlot.transform.eulerAngles.z);
 
         mouseDir = new Vector2 (mousePosition.x - weaponSlot.transform.position.x, mousePosition.y - weaponSlot.transform.position.y);
 
         //sets up the horizontal and vertical Vector buttons
         movement = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
 
         //movement if horizontal or vertical buttons is pressed
         rbody2d.MovePosition (rbody2d.position + movement * moveSpeed * Time.deltaTime);
     }
 }
if anyone can help me you would really make my day. i have been stuck on this for so long and it was just something i noticed today right when i though my next objective was to make some levels :P
Answer by samsut · Jun 20, 2017 at 06:45 PM
Well, I have this. Put this script in the object you want to move.
     public Transform controlThisObject; //Here can be the transform of the weapon
     private Vector3 mousePos;
 
     void Update(){
        MouseL();
     }
 
     //Mouse Look script
     void MouseL(){
         //Gets mouse position, you can define Z to be in the position you want the weapon to be in
         mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
         Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
         lookPos = lookPos - transform.position;
         float angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
It'll make an object rotate with the mouse pointer(the object will face it), the sprite/object will be facing to the right at the start. It works for me. I use in a top down 2d game, is a ship game.
Your answer
 
 
             Follow this Question
Related Questions
Shoot bullet towards mouse 0 Answers
Is there any way to trigger a popUp UI when a player steps in a specific set of tiles? 0 Answers
2D fixed cam aim problem 0 Answers
How do I make it so that two player controlled objects of varying speeds stay close to each other? 0 Answers
New user question about 2D top down game and collision. 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                