Search Unity

How do I look at the Mouse Position? (3d)

Discussion in 'Scripting' started by Jeffey_101, Jul 29, 2018.

  1. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Hi, I'm new to Unity and trying to make a top-down, 3d dungeon crawler game(Using C#). I have the player movement down and the camera following the player. But how do I make the character look at where the mouse is?

    This is the script I currently have, found it on another thread but didn't work for me.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class LookAtMouse : MonoBehaviour {


    void Update () {
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    var mousePosition = new Vector3(ray.origin.x, transform.position.y, ray.origin.z);

    transform.LookAt(mousePosition);
    }
    }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Are you wanting something like this? (Also, please son't forget to use Code Tags when including code, it is easier on the eye :)) :-
    Code (CSharp):
    1. public class LookAtMouse : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         m_camera = Camera.main;  // Don't keep calling Camera.main
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         var lookAtPos = Input.mousePosition;
    11.         lookAtPos.z = transform.position.z - m_camera.transform.position.z;
    12.         lookAtPos = m_camera.ScreenToWorldPoint(lookAtPos);
    13.         transform.up = lookAtPos - transform.position;
    14.     }
    15.  
    16.     Camera m_camera;
    17. }
     
    MrChongo likes this.
  3. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    The code you gave me didn't quite work, kept getting an error saying - NullReferenceException: Object reference not set to an instance of an object LookAtMouse.Update () (at Assets/LookAtMouse.cs:13)
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Can you post the code back as you've written it so I can check what you've got.
     
  5. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LookAtMouse : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         m_camera = Camera.main;  // Don't keep calling Camera.main
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         var lookAtPos = Input.mousePosition;
    13.         lookAtPos.z = transform.position.z - m_camera.transform.position.z;
    14.         lookAtPos = m_camera.ScreenToWorldPoint(lookAtPos);
    15.         transform.up = lookAtPos - transform.position;
    16.     }
    17.  
    18.     Camera m_camera;
    19. }
    I only copy-pasted it into visual studio.
    If there's something else i need to do to make it work, I'm all ears.
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I presume you have a camera in your scene? If so, then can you debug line 13 and find out what exactly is null.
     
  7. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Sorry, but how exactly do you debug the code? (New to this stuff, thanks for helping though)
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    No worries. Are you using Visual Studio?
     
  9. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    I am using Visual Studio. Also, I was messing around a bit and changed the tag from untagged to MainCamera. That got rid of the error, but it still isn't accomplishing the effect I want it to; actually, it didn't affect anything.
     
  10. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    The tag for my camera.
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do you mean "Main Camera" (with the space)?

    So this script is on your GameObject and that object is in the scene and enabled?
     
  12. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    The tag in Unity is called "MainCamera" without a space. (I had renamed the camera to "Player Camera" if that changes anything.)
    The script is attached to a GameObject designated as "Player" and the object is indeed enabled.
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Can you add the space and try again. Just tested it again and it works ok for me.
     
  14. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    I added the space for the tag and that made something happen. But it's not exactly what I'm looking for. Moving the mouse around just makes the character fall over in different directions. What I want to happen is have the WASD keys be the movement and have the mouse location be how the character is oriented (Rotating only on the Y axis). The camera follows the character/player around the environment.
     
  15. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to check : on your screen, the X axis runs horizontally, the Y axis vertically and the Z axis directly into the screen. Is that correct?
     
  16. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Yes
     
  17. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I understand that you are writing a top-down game. So do you not want the rotation of the sprites around the Z axis?
     
  18. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    No, only the Y axis (So the character would spin in circles if you spun the mouse around).
     
  19. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So when the sprite (we are talking about 2D sprites here, right?) is rotated at 90 degrees it would be invisible as you would be looking at it edge on. Are you sure you want it rotating around the Y axis? Am I missing something? :)
     
  20. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    It is actually a 3d top down game.
     
  21. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, but "top down" means we are God-like looking down on everything. Also, we have already agreed that we are looking along the Z axis (that runs into the screen).

    Therefore the ground we can see that stretches to our left and right is running along the X-axis. The ground that we see that stretches up and down the screen is along the Y-axis. So when the player stand on the ground their head is closer to us along the Z-axis than are their feet.

    When the player moves forward, they will move along the X and Y axes. If they were to jump, they would move along the Z axis.

    Therefore, if they were to turn around in a circle with their feet remaining firmly on the ground, they would be rotating about their Z axis, would they not?
     
  22. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Maybe I misunderstood when you asked how my axes were oriented. From the players' cameras' perspective, we are looking down along the Y, and the ground is along the X and Z. When you asked that " the X axis runs horizontally, the Y axis vertically and the Z axis directly into the screen," I thought you just meant for the Scene, not the Game. (Probably explained it poorly, if so that's my bad.)
     
  23. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, the thing is that is sounding more like a "first person" view than "top down"? I'm not meaning to harp on, I'm just trying to understand the setup you have to see how we can adjust the code I gave to make it work for your situation. :)
     
  24. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    No, you're fine. The camera is positioned above the character looking "down" along the Y axis. It is offset by 10 on the Y axis. The camera is angled 90 degrees down along the X axis. (If you need me to explain more, just lmk).
     
  25. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Maybe a picture could be helpful?
     
  26. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, let's try this attempt :) :-
    Code (CSharp):
    1. public class LookAtMouse : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         m_camera = Camera.main;  // Don't keep calling Camera.main
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         var lookAtPos = Input.mousePosition;
    11.         lookAtPos.z = m_camera.transform.position.y - transform.position.y;
    12.         lookAtPos = m_camera.ScreenToWorldPoint(lookAtPos);
    13.         transform.forward = lookAtPos - transform.position;
    14.     }
    15.  
    16.     Camera m_camera;
    17. }
     
    Lyndon4 and Justice0Juic3 like this.
  27. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    Wow, thanks! That was exactly what I was looking for.
     
    Doug_B likes this.
  28. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Cool. :cool:
     
  29. AppleJuiceFlood

    AppleJuiceFlood

    Joined:
    Jun 21, 2017
    Posts:
    1
    Doug,

    Thanks dude! You helped someone else years later!