Search Unity

Survival Shooter Character Doesn't Rotate with mouse Inpput

Discussion in 'Getting Started' started by vidit0210, Jan 11, 2016.

  1. vidit0210

    vidit0210

    Joined:
    Dec 30, 2015
    Posts:
    30
    Hello,

    My Character Doesn't rotate while taking input from mouse however it can move with keyboard keys.
    I checked my code twice.
    Copied the code from instructor and posted still it doesn't rotates.
    Help needed.

    Thanking You.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Post your code here (using code tags, please) and a screenshot or two of the Inspector panel for relevant objects. Also, which section of the tutorial you're currently working on would be helpful, too.
     
  3. vidit0210

    vidit0210

    Joined:
    Dec 30, 2015
    Posts:
    30
    Sorry for providing vague information.
    PLayers inspector.

    Screen Shots:



    code: Same one
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;            // The speed that the player will move at.
    6.  
    7.     Vector3 movement;                   // The vector to store the direction of the player's movement.
    8.     Animator anim;                      // Reference to the animator component.
    9.     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    10.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    11.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    12.  
    13.     void Awake ()
    14.     {
    15.         // Create a layer mask for the floor layer.
    16.         floorMask = LayerMask.GetMask ("Floor");
    17.  
    18.         // Set up references.
    19.         anim = GetComponent <Animator> ();
    20.         playerRigidbody = GetComponent <Rigidbody> ();
    21.     }
    22.  
    23.  
    24.     void FixedUpdate ()
    25.     {
    26.         // Store the input axes.
    27.         float h = Input.GetAxisRaw ("Horizontal");
    28.         float v = Input.GetAxisRaw ("Vertical");
    29.  
    30.         // Move the player around the scene.
    31.         Move (h, v);
    32.  
    33.         // Turn the player to face the mouse cursor.
    34.         Turning ();
    35.  
    36.         // Animate the player.
    37.         Animating (h, v);
    38.     }
    39.  
    40.     void Move (float h, float v)
    41.     {
    42.         // Set the movement vector based on the axis input.
    43.         movement.Set (h, 0f, v);
    44.        
    45.         // Normalise the movement vector and make it proportional to the speed per second.
    46.         movement = movement.normalized * speed * Time.deltaTime;
    47.  
    48.         // Move the player to it's current position plus the movement.
    49.         playerRigidbody.MovePosition (transform.position + movement);
    50.     }
    51.  
    52.     void Turning ()
    53.     {
    54.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    55.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    56.  
    57.         // Create a RaycastHit variable to store information about what was hit by the ray.
    58.         RaycastHit floorHit;
    59.  
    60.         // Perform the raycast and if it hits something on the floor layer...
    61.         if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    62.         {
    63.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    64.             Vector3 playerToMouse = floorHit.point - transform.position;
    65.  
    66.             // Ensure the vector is entirely along the floor plane.
    67.             playerToMouse.y = 0f;
    68.  
    69.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    70.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    71.  
    72.             // Set the player's rotation to this new rotation.
    73.             playerRigidbody.MoveRotation (newRotation);
    74.         }
    75.     }
    76.  
    77.     void Animating (float h, float v)
    78.     {
    79.         // Create a boolean that is true if either of the input axes is non-zero.
    80.         bool walking = h != 0f || v != 0f;
    81.  
    82.         // Tell the animator whether or not the player is walking.
    83.         anim.SetBool ("IsWalking", walking);
    84.     }
    85. }
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    When you run it do you then click the mouse inside the game view window? We've found that we've had to do that sometimes, sort of like telling it that you want it to start using the mouse in the game & not in the editor.
     
    Downson likes this.
  5. vidit0210

    vidit0210

    Joined:
    Dec 30, 2015
    Posts:
    30
    Yes,Still does'nt work out.
     
  6. MissDaLuz

    MissDaLuz

    Joined:
    Sep 11, 2015
    Posts:
    1
    Did you ever get a response to this that worked? Mine still doesn't work.
     
  7. mattparkins

    mattparkins

    Joined:
    Apr 1, 2014
    Posts:
    31
    I fixed this by setting the Floor quad's z scale to 1, previously it was set to 0.

    I'm not sure how it got set to zero, but presumably RayCasts can't hit a quad with a 0 in any of the scale vars.
     
    imti-uz likes this.
  8. TrevorBOB

    TrevorBOB

    Joined:
    Aug 24, 2016
    Posts:
    1
    I'm having this same problem, Clicking and setting the quad's z scale to 1 didn't work
     
  9. Leohd

    Leohd

    Joined:
    Feb 7, 2017
    Posts:
    32
    If you also have a "null" error while your game is running, it's possible your camera is not tagged as the MainCamera in inspector. Check the tag.
     
    moryumii likes this.
  10. Dmdldld

    Dmdldld

    Joined:
    Dec 10, 2017
    Posts:
    1
    I found the problem! perhaps, Floor's Layer is "Default" which in the Hierarchy. You must change it to Floor.
    I hope you already have fixed this problem.
     
    yosshy0805 and braithw8 like this.
  11. braithw8

    braithw8

    Joined:
    Jan 27, 2018
    Posts:
    1
    Thanks Dmdldld!
     
  12. Joshuapickering767

    Joshuapickering767

    Joined:
    Apr 17, 2018
    Posts:
    1
    mine still doesn't work i tried everything on here
     
    Moonwink likes this.
  13. Ahmad987

    Ahmad987

    Joined:
    Aug 31, 2018
    Posts:
    1
    everything is just in accordance with tutorial. But still no rotation.... may be there might be some version issue...??? mine was 2018.2.5
     
  14. john2260

    john2260

    Joined:
    Sep 6, 2018
    Posts:
    1
    If you also have a "null" error while your game is running, it's possible your camera is not tagged as the MainCamera in inspector. Check the tag. This fixed mine thanks!!!!!
     
    chantaraw likes this.
  15. mjedyny

    mjedyny

    Joined:
    Feb 25, 2019
    Posts:
    1
    In my case the rotation didn't work for the main camera in its default position. After I moved it up a little bit it works fine