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
0
Question by kitallen23 · Oct 09, 2014 at 07:54 AM · cursorisometriclook

Isometric game, player look at cursor?

Hi! I'm going crazy trying to figure out how to make my player (a simple cube) always look at the cursor. I've tried a few things, including various modifications to suggestions in this post.

My basic setup is an isometric game with a cube character. Here is a screenshot of the setup:

alt text

My character, the cube, has the ability to move along the X and the Z axis - he will always be locked in the same Y coordinate (sitting on the flat ground). My attempts have mainly been using something similar to this:

 void Start()
     {
         cameraDif = camera.transform.position.y - player.transform.position.y;
     }
 
     void Update ()
     {
         player.transform.LookAt(new Vector3(Input.mousePosition.x, cameraDif, Input.mousePosition.z));
     }

Any help would be greatly appreciated! Thanks :)

perspective.png (51.0 kB)
Comment
Add comment · Show 5
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 alap soni · Oct 09, 2014 at 09:01 AM 0
Share

try using screentoworldpoint concept

avatar image kitallen23 · Oct 09, 2014 at 09:35 AM 0
Share

Hi Alap Soni, I've experimented with that a fair bit and had no success. $$anonymous$$y code was this:

     private float cameraDif;
     public Rigidbody player;
     private float mouseX;
     private float mouseZ;
     private Vector3 worldPos;
 
     void Start()
     {
         cameraDif = camera.transform.position.y - player.transform.position.y;
     }
 
     void Update ()
     {
         mouseX = Input.mousePosition.x;
         mouseZ = Input.mousePosition.z;
 
         worldPos = camera.ScreenToWorldPoint(new Vector3(mouseX, cameraDif, mouseZ));
         
         player.transform.LookAt(worldPos);
     }

It's almost exactly the same as the code used in one of the comments in the thread here. Have I done something wrong/adapted it to my situation incorrectly, or will it just not work in my situation?

avatar image Chubzdoomer · Oct 09, 2014 at 09:48 AM 0
Share

Have you by any chance tried raycasting, then using the LookAt function (or something similar) to make your cube rotate and face towards the raycast's hit?

In the link you provided in your initial post, one of the first responses uses that method, so I was just curious whether or not you'd already given it a shot.

avatar image HarshadK · Oct 09, 2014 at 09:51 AM 0
Share

There is a great article that caters around the Isometric game development which shows how to convert 2D view (screen co-ordinates) to 2.5D something along the line of what you want: Creating Isometric Worlds: A Primer for Game Developers

avatar image kitallen23 · Oct 09, 2014 at 01:19 PM 0
Share

Chubzdoomer, I've tried to decipher that code you're referring to, but could not. I am quite new to coding, and struggle to understand JS at this point in time. If possible, could you help translate it to C#? Still struggling with this.

3 Replies

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

Answer by Chubzdoomer · Oct 09, 2014 at 07:51 PM

Here's a quick script I wrote that seems to do the trick. All it does is perform a raycast from the main camera to the mouse cursor and beyond. If the raycast strikes anything you've tagged as "Ground," the cube will then rotate to face towards the point of the hit.

 public class CubeScript : MonoBehaviour 
 {
     Ray cameraRay;                // The ray that is cast from the camera to the mouse position
     RaycastHit cameraRayHit;    // The object that the ray hits
 
     void Update () 
     {
         // Cast a ray from the camera to the mouse cursor
         cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         // If the ray strikes an object...
         if (Physics.Raycast(cameraRay, out cameraRayHit)) 
         {
             // ...and if that object is the ground...
             if(cameraRayHit.transform.tag=="Ground")
             {
                 // ...make the cube rotate (only on the Y axis) to face the ray hit's position 
                 Vector3 targetPosition = new Vector3(cameraRayHit.point.x, transform.position.y, cameraRayHit.point.z);
                 transform.LookAt(targetPosition);
             }
         }
     }
 }

I'm sure the code to be improved in several ways (like making the rotation occur over time rather than instantly), but this should at least help get you started.

I hope this helps, and best of luck!

Comment
Add comment · Show 1 · 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 Spectrr · Mar 04, 2021 at 11:40 AM 0
Share

I'm currently doing something similar, is there a noticeable performance issue if you remove the "need" for the function to hit the "ground" and have the sprite constantly face the mouse, since it will be updating to the mouse position consistently?

avatar image
0

Answer by TheShadyColombian · Oct 14, 2014 at 09:23 PM

try the method they use in the Nightmares tutorial in Unite 2014. you can find the tutorial in youtube in the Unity channel.

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 BarthaSzabolcs · May 02, 2021 at 10:55 AM

I made a tutorial about this, it's basically the same solution, but I try to explain how it works in the video.

I will leave it here, might help somebody. :)

https://www.youtube.com/watch?v=AOVCKEJE6A8

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

7 People are following this question.

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

Related Questions

Movement along X and Z axis... 2 Answers

Look at the mouse? 2 Answers

Make player look at pointed target 1 Answer

How do I get my character to look in the direction of the cursor? 1 Answer

UI input field caret position wrong when Centre alignment unity 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