Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character rotating into mouse direction WITHOUT using raycast?

Discussion in 'Scripting' started by Evangeder, May 7, 2014.

  1. Evangeder

    Evangeder

    Joined:
    Feb 5, 2014
    Posts:
    9
    Hey!
    Is there any way to force the character looking at mouse without raycasting?
    Why is that? It's because my camera is placed at semi-isometric position, and when i use the raycast code - my character is looking at all the walls and acts weirdly when it's above the ground. Sometimes it glitches THAT much, that when i have my mouse below character it still look upwards.

    Any idea how to solve this?

    Here's the code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseLooking : MonoBehaviour {
    5.     public float Speed = 10;
    6.     Vector3 position;
    7.        
    8.     void Update () {
    9.         //if (Input.GetMouseButton (0)){
    10.             RayTrace();
    11.         //}
    12.         MoveToPosition();
    13.     }
    14.  
    15.     void RayTrace() {
    16.         RaycastHit hit;
    17.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    18.         if (Physics.Raycast(ray, out hit, 1000)) {
    19.             position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    20.             //Debug.Log(position);
    21.         }
    22.     }
    23.  
    24.     void MoveToPosition(){
    25.         if (Vector3.Distance(transform.position, position) > 5) {
    26.             Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
    27.            
    28.             newRotation.x = 0f;
    29.             newRotation.z = 0f;
    30.            
    31.             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 30);
    32.         }
    33.     }
    34. }
    35.  
    Thank you <3
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Why are you adjusting the x and z of the quaternion?

    I am sure that would give you incredibly strange results.


    So I took out the offending stuff and played with it some.
    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.      
    5.     public class MouseLooking : MonoBehaviour {
    6.         public float Speed = 10;
    7.         Vector3 position;
    8.            
    9.         void Update () {
    10.             if (Input.GetMouseButtonDown (0)){
    11.                 GetPoint();
    12.             }
    13.             RotateToLook();
    14.         }
    15.      
    16.         void GetPoint() {
    17.             RaycastHit hit;
    18.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.             if (Physics.Raycast(ray, out hit, 1000)) {
    20.                 position = hit.point;
    21.                 //Debug.Log(position);
    22.             }
    23.         }
    24.      
    25.         void RotateToLook(){
    26.             if (Vector3.Distance(transform.position, position) > 5) {
    27.                 Quaternion rotation = transform.rotation;
    28.                
    29.                 // ensure that we are looking at the same plane as the character
    30.                 //Vector3 p = position;
    31.                 //p.y = transform.position.y;
    32.                 //transform.LookAt(p);
    33.                
    34.                 transform.LookAt(position);
    35.                
    36.                
    37.                 transform.rotation = Quaternion.Slerp(rotation, transform.rotation, Time.deltaTime * 30);
    38.             }
    39.         }
    40.     }
    41.  
    and of course I know what you are trying to do, so I renamed the functions to say what they do. MoveToPosition Rotates? ;)
     
  4. Evangeder

    Evangeder

    Joined:
    Feb 5, 2014
    Posts:
    9
    I want to rotate it only by Y, not the X and Z ;)

    Also, this script does the same thing as mine, which is not correct. I'll post screens what i mean:

    http://i.imgur.com/AutshHc.png
    http://i.imgur.com/1Zym0WH.png

    This is what i mean. I want the cube to only look at ground direction, like.. forcing my mouse to no-clip through the wall, and then make the raycast.
    I cannot get past this :c

    And the "MoveToPosition" was because it was firstly moving like in diablo, but then i decided to make WSAD movement ;)
     
  5. Miberen

    Miberen

    Joined:
    May 9, 2014
    Posts:
    1
    I think what he meant with the X, Y, Z is that you generally don't wanna mess with individual components of a quaternion as it has to be normalized and everything, can lead to some strange stuff.

    In regards to what the problem you showed in the pictures, i think you can make a layermask and apply it to the raycast, then make sure only the ground has a specific "ground" layer or something on it, raycast should only hit the ground or whatever other objects you put the layer on.