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 /
avatar image
0
Question by 13yearoldceo · Oct 09, 2015 at 01:12 PM · mousepositionmouselookscreentoworldpointtop down shootermouse aim

Top Down shooter mouse controls

Hello guys well I am creating a top down 2d shooter, and i have ran in to a problem with my controls. I want to make my player look at the mouse direction in order to be able to aim, but for some reason it seems to be always off from where the mouse is pointing at only in two certain positions does the mouse and player line up. I have a feeling that unity is getting my mouse coordinates wrong when i transfer it to real world coordinates but I am not sure. please help thank you.

 using UnityEngine;
 using System.Collections;
 
 public class playermovementscript : MonoBehaviour { 
 
     public float movementspeed = 5f; 
     public float bulletspeed  = 10f;
     public GameObject bullet; 
     public GameObject shooter;  
     public Vector3 target;  
     public Camera camera;
 
     // Use this for initialization
     void Start () { 
         Camera camera;
     
     }
     
     // Update is called once per frame
     void Update () {
      
         movement (); 
 
         if (Input.GetKeyDown (KeyCode.F)) {
             shoot();
         } 
 
 } 
 
 
 
 
     void movement(){
 
         
         gameObject.GetComponent<Rigidbody>().velocity = new Vector3(Input.GetAxisRaw("Horizontal")*movementspeed,0,Input.GetAxisRaw("Vertical")*movementspeed); 
 
     
         target = camera.ViewportToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.nearClipPlane)); 
 
 
         Debug.Log (target); 
         transform.LookAt (target,Vector3.up);
     }  
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by 13yearoldceo · Oct 10, 2015 at 01:15 PM

Replace" Viewporttoworldpoint" with "screentoworldpoint"

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
avatar image
0

Answer by Chris-Paul · Oct 09, 2015 at 01:28 PM

Here is the code for your "movement" method: (you needed to cast a ray to that object, to know it's actually there)

         Rigidbody rigidbody = GetComponent<Rigidbody>();
 
         // Velocity Asigning.
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
         rigidbody.velocity = new Vector3(h, 0, v) * movementspeed;
 
         // Getting the mouse position in screen space
         // and make a ray from it.
         Vector2 mousePosition = Input.mousePosition;
         Ray ray = camera.ScreenPointToRay(new Vector3(mousePosition.x, mousePosition.y));
 
         // Actually cast the ray and see if any object was hit.
         RaycastHit hitInfo;
         if(Physics.Raycast(ray.origin, ray.direction, out hitInfo)) {
             // If we hit something with our ray, then that means we have a target;
             target = hitInfo.collider.transform.position;
         }
 
         // Look at the target;
         transform.LookAt(target);
Comment
Add comment · Show 2 · 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
avatar image 13yearoldceo · Oct 09, 2015 at 08:40 PM 0
Share

Thank you, but i am trying to avoid using rays for now, but even if i do what if my ray does not anything.

avatar image 13yearoldceo · Oct 10, 2015 at 01:14 PM 0
Share

Don't worry I've saw where i went wrong i accidentally used "viewporttoworldpoint" ins$$anonymous$$d of "screentoworldpoint" thank you for the help anyways.

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

30 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

Related Questions

Move Object - Mouse Input Y Axis from Camera 0 Answers

Wrong world point mouse position 1 Answer

Rotate sprite to face direction of movement 1 Answer

Convert Mouse Position to move a cube on 1-axis. 1 Answer

ScreenToWorldPoint error 3 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