Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by awplays49 · May 11, 2015 at 09:27 PM · mouse look

Top down shooter mouse look (3d)

How can i make a top down shooter mouse look? (xz movement, y rotation) this is what i have:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     private Vector3 mousePos;
     private float lastAngle;
     void Update () {
         mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.y));
     }
 }
 
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
2
Best Answer

Answer by WillNode · May 11, 2015 at 09:47 PM

not testing this script, but i hope this one is working well.

 void Update () {
     mousePos = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y, Camera.main.transform.position.z-transform.position.z));
     transform.LookAt (mousePos);
      }

if not working, maybe this one :

   void Update () {
          mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
          transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.z));
      }


Comment
Add comment · Show 6 · 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 awplays49 · May 11, 2015 at 09:51 PM 0
Share

I tried the bottom, nothing happens.

avatar image awplays49 · May 11, 2015 at 09:53 PM 0
Share

ill try the other.

avatar image awplays49 · May 11, 2015 at 09:54 PM 0
Share

top works! thanks!

avatar image WillNode · May 11, 2015 at 09:59 PM 0
Share

O$$anonymous$$, if this works for you, maybe you can mark this as accepted :)

avatar image awplays49 · May 11, 2015 at 10:09 PM 0
Share

No problem! :)

Show more comments
avatar image
1

Answer by BarthaSzabolcs · May 04, 2021 at 06:03 PM

I know it's an old post, but this is still a valid problem.


I made a 3 minute tutorial about this with explanation and an example project.


My Solution

 using UnityEngine;
 
 namespace BarthaSzabolcs.IsometricAiming
 {
     public class IsometricAiming : MonoBehaviour
     {
         #region Datamembers
 
         #region Editor Settings
 
         [SerializeField] private LayerMask groundMask;
 
         #endregion
         #region Private Fields
 
         private Camera mainCamera;
 
         #endregion
 
         #endregion
 
 
         #region Methods
 
         #region Unity Callbacks
 
         private void Start()
         {
             // Cache the camera, Camera.main is an expensive operation.
             mainCamera = Camera.main;
         }
 
         private void Update()
         {
             Aim();
         }
 
         #endregion
 
         private void Aim()
         {
             var (success, position) = GetMousePosition();
             if (success)
             {
                 // Calculate the direction
                 var direction = position - transform.position;
 
                 // You might want to delete this line.
                 // Ignore the height difference.
                 direction.y = 0;
 
                 // Make the transform look in the direction.
                 transform.forward = direction;
             }
         }
 
         private (bool success, Vector3 position) GetMousePosition()
         {
             var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundMask))
             {
                 // The Raycast hit something, return with the position.
                 return (success: true, position: hitInfo.point);
             }
             else
             {
                 // The Raycast did not hit anything.
                 return (success: false, position: Vector3.zero);
             }
         }
 
         #endregion
     }
 }


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 SpaceManDan · May 21, 2021 at 08:52 PM 1
Share

Elegant! This is the correct way to do this. :)

avatar image BarthaSzabolcs SpaceManDan · May 22, 2021 at 12:49 PM 0
Share

Thanks! :)

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

23 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

Related Questions

Explaining A Mouse Look Script 1 Answer

gyro to mouse look 0 Answers

My camera flickers and it's driving me crazy 2 Answers

Main Camera won't rotate left or right 0 Answers

"Ref Float" for Mouse Look script? 1 Answer


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