The question is answered
Player doesn't turn
Hi there,
I'm using Unity tutorials to make a game and in this part - https://www.youtube.com/watch?v=R8O8Y6xP79w - I got issue because my player doesn't turn there where my mouse is. Here is my code for turning:
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
}
I was trying to find issue on my own and everything I found as other people was fixing this is that they either didn't have correct scale for quad which is floor - 100 x 100 x 1 - or they didn't have Z scale at least 1, usually they had 0 or layer of quad which is floor was set up to defalut instead floor. I've had all of these setting already but my player still does not turn where my mouse it. Any ideas?
The general scale of the floor does not have to do with anything here. The direction of the mesh yes, you need to have the y (or z, depending on your mesh, the "up" direction) scale at 1. Otherwise the floor mesh is inverted. A normal raycast will not hit the backface of meshes.
Reading through your whole script, I too see no reason why it shouldn't work as indented, the only thing that seems to be the possible culprit to me is the layer section. Are you sure the floor objects are on the correct layer?
Why do you need a Layer$$anonymous$$ask? If the clicked floor point is behind a wall maybe? $$anonymous$$ake sure the ray works without the mask first, then add it. It's good to start with the needed arguments of functions and later add the optional ones if needed. That way you can prevent searching for errors later ;)
$$anonymous$$aybe add a print ("Hit");
check into the raycast section to be absolutely sure it hits anything.
As in the tutorial I made x and y scale to 100 and z scale to 1. I see while changing x and y, mesh collider is growing bigger and while changing z scale is nothing changing so scaling is surely good. However, I noticed rotation X which is set up to 90 in tutorial after start game, at me, it's making immediately to "89.98019" and I don't know why it's changing as before start game it doesn't change and if there could be problem in this.
Let's take a look on this all: https://zapodaj.net/4f9601f68f946.png.html
I actually don't understand and remember yet why I do need Layer$$anonymous$$ask there but as I said I'm doing this with Unity tutorial and they added this as well, although, after remove it, everything worked that same, I think.
I added print ("Hit");
but there was no messages, lol. But maybe I add it in incorrect place in code, exactly at the end of turning and include "if" in turning, I don't know if I done it correct and maybe if not then it didn't show me anything.
Don't worry about the 89.999 it's a floating point which have a level of flexibility so it just changed on it's own via the engine.
Your hit variable is floorHit so you'd need print ("floorHit");
to get a result or change the variable to Hit.
So we now know part of why it's not turning. This is called diagnostics. :)
Now why doesn't it hit anything, we know the ray aims at the mouse pointer from the camera so make sure that the pointer is on the floor and if it still isn't looking at where the mouse pointer is then try moving the camera higher up and tilt it towards the character.
Where can I check where is pointer? Sorry for (as I guess) dumb question but I am completely newbie in this for now :)
The mouse pointer is the mouse arrow that moves around when you move the mouse. The character should turn to look towards it as in the tutorial. EDIT - well if the pointer is on the floor area anyway.
Difficult to tell from that picture, try moving the Camera so it's inside the room and looking down, at an angle, towards the player.
I did this and it was that same but I fixed it! I didn't tag camera to $$anonymous$$ainCamera. After tag it everything works. I can go to the next tutorial :D Thank you so much for your patient to me and helping, ofc :D
Answer by Mmmpies · Jun 02, 2016 at 08:46 PM
That part of your script looks fine but watch the last 5 minutes of that tutorial. They mention that Awake and FixedUpdate and some other functions get called automatically but other functions, that you create, don't unless they're called. I think you're not calling
Turning();
In FixedUpdate or there's a typo somewhere.
Of course I'm watching all tutorials and this thing I did already have written.
Btw here is all my code if you'd like to check everything:
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floor$$anonymous$$ask;
float camRayLenght = 100f;
void Awake()
{
floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Floor");
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
$$anonymous$$ove(h, v);
Turning();
Animating(h, v);
}
void $$anonymous$$ove(float h, float v)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.$$anonymous$$ovePosition(transform.position + movement);
}
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, camRayLenght, floor$$anonymous$$ask))
{
Vector3 playerTo$$anonymous$$ouse = floorHit.point - transform.position;
playerTo$$anonymous$$ouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerTo$$anonymous$$ouse);
playerRigidbody.$$anonymous$$oveRotation(newRotation);
}
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
}
It's getting pretty late where I am so for clarification is movement other than turning working?
Hi again, move Q's like these to Help please if you choose to reply to them ;)
Follow this Question
Related Questions
Health Script 2 Answers
Why cant I access the same function twice? 0 Answers
How do I create a Continuous Turn in XR for my VR Rig instead of Snap Turn Provider? 0 Answers
How can i make sure the script will work only on the attached object ? 1 Answer
How can I check if a object in the hierarchy have been destroyed using EditorWindow type script ? 1 Answer