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:
using UnityEngine;
using System.Collections;
public class MouseLooking : MonoBehaviour {
public float Speed = 10;
Vector3 position;
void Update () {
//if (Input.GetMouseButton (0)){
RayTrace();
//}
MoveToPosition();
}
void RayTrace() {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000)) {
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
//Debug.Log(position);
}
}
void MoveToPosition(){
if (Vector3.Distance(transform.position, position) > 5) {
Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 30);
}
}
}
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.
using UnityEngine;
using System.Collections;
public class MouseLooking : MonoBehaviour {
public float Speed = 10;
Vector3 position;
void Update () {
if (Input.GetMouseButtonDown (0)){
GetPoint();
}
RotateToLook();
}
void GetPoint() {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000)) {
position = hit.point;
//Debug.Log(position);
}
}
void RotateToLook(){
if (Vector3.Distance(transform.position, position) > 5) {
Quaternion rotation = transform.rotation;
// ensure that we are looking at the same plane as the character
//Vector3 p = position;
//p.y = transform.position.y;
//transform.LookAt(p);
transform.LookAt(position);
transform.rotation = Quaternion.Slerp(rotation, transform.rotation, Time.deltaTime * 30);
}
}
}
and of course I know what you are trying to do, so I renamed the functions to say what they do. MoveToPosition Rotates?
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
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.