Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Jamiemsm · Mar 15, 2017 at 11:29 PM · 2d gamemousepositiontopdown

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.

alt text

alt text

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

asd.png (435.2 kB)
asdasd.png (443.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges