• 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 /
  • Help Room /
This question was closed Jun 12, 2016 at 11:32 PM by Le-Pampelmuse for the following reason:

The question is answered

avatar image
0
Question by LoKo6264 · Jun 02, 2016 at 07:16 PM · c#script.mouse positionturning

Player doesn't turn

Hi there,

I'm using Unity tutorials to make a game and in t$$anonymous$$s 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 everyt$$anonymous$$ng I found as other people was fixing t$$anonymous$$s is that they either didn't have correct scale for quad w$$anonymous$$ch 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 w$$anonymous$$ch 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?

Comment
Add comment · Show 11
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 Le-Pampelmuse · Jun 03, 2016 at 03:15 AM 1
Share

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 LayerMask? If the clicked floor point is behind a wall maybe? Make 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 ;)

Maybe add a print ("Hit"); check into the raycast section to be absolutely sure it hits anything.

avatar image LoKo6264 Le-Pampelmuse · Jun 03, 2016 at 12:46 PM 0
Share

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 LayerMask 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.

avatar image Mmmpies LoKo6264 · Jun 03, 2016 at 01:15 PM 0
Share

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.

Show more comments
avatar image Mmmpies · Jun 03, 2016 at 02:35 PM 0
Share

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.

avatar image LoKo6264 Mmmpies · Jun 03, 2016 at 03:30 PM 0
Share

Where can I check where is pointer? Sorry for (as I guess) dumb question but I am completely newbie in this for now :)

avatar image Mmmpies LoKo6264 · Jun 03, 2016 at 03:38 PM 0
Share

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.

Show more comments
avatar image Mmmpies · Jun 03, 2016 at 07:27 PM 0
Share

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.

avatar image LoKo6264 Mmmpies · Jun 03, 2016 at 07:38 PM 1
Share

I did this and it was that same but I fixed it! I didn't tag camera to MainCamera. 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

avatar image Mmmpies LoKo6264 · Jun 03, 2016 at 07:41 PM 0
Share

Woo hoo

\o/

Glad you fixed it :¬)

1 Reply

  • Sort: 
avatar image
0

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 t$$anonymous$$nk you're not calling

 Turning();

In FixedUpdate or there's a typo somewhere.

Comment
Add comment · Show 5 · 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 LoKo6264 · Jun 02, 2016 at 09:19 PM 0
Share

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 PlayerMovement : MonoBehaviour
 {
     public float speed = 6f;
     Vector3 movement;
     Animator anim;
     Rigidbody playerRigidbody;
     int floorMask;
     float camRayLenght = 100f;
 
     void Awake()
     {
         floorMask = LayerMask.GetMask("Floor");
         anim = GetComponent<Animator>();
         playerRigidbody = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
 
         Move(h, v);
         Turning();
         Animating(h, v);
 
     }
 
     void Move(float h, float v)
     {
         movement.Set(h, 0f, v);
         movement = movement.normalized * speed * Time.deltaTime;
         playerRigidbody.MovePosition(transform.position + movement);
     }
 
     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);
         }
     }
 
     void Animating(float h, float v)
     {
         bool walking = h != 0f || v != 0f;
         anim.SetBool("IsWalking", walking);
     }
 }
avatar image Mmmpies LoKo6264 · Jun 02, 2016 at 09:32 PM 0
Share

It's getting pretty late where I am so for clarification is movement other than turning working?

avatar image LoKo6264 Mmmpies · Jun 02, 2016 at 09:48 PM 0
Share

Everything works fine except turning.

avatar image Le-Pampelmuse · Jun 03, 2016 at 02:54 AM 2
Share

Hi again, move Q's like these to Help please if you choose to reply to them ;)

avatar image LoKo6264 Le-Pampelmuse · Jun 03, 2016 at 12:47 PM 0
Share

I'll remember, sir! :)

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

154 People are following this question.

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

Related Questions

,UNEXPECTED SYMBOL VAR 1 Answer

How to rotate an object 2 Answers

How can i spawn random number of prefabs in random positions with gap of 10 inside a rectangle area ? 2 Answers

Is it possible to make an Okami Brush mechanic in Unity Engine? 0 Answers

Destroying enemy only partially works 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