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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character look to mouse

Discussion in 'Scripting' started by Synder, Oct 7, 2015.

  1. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    I am trying to get my character to look to where my mouse is pointing while using the wasd keys for movement. I have tried several different scripts but none have worked. I'm not sure what it is that is hindering my character from turning. Any suggestions?
     
  2. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     Vector3 movement;
    14.     public Boundary boundary;
    15.     new Rigidbody rigidbody;
    16.     int floorMask;
    17.     float camRayLength = 100f;
    18.  
    19.     public GameObject shot;
    20.     public Transform shotSpawn;
    21.     public float fireRate;
    22.  
    23.     private float nextFire;
    24.  
    25.     void Update()
    26.     {
    27.        
    28.         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
    29.             nextFire = Time.time + fireRate;
    30.             Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
    31.             GetComponent<AudioSource>().Play ();
    32.         }
    33.     }
    34.  
    35.     void Start()
    36.     {
    37.  
    38.         floorMask = LayerMask.GetMask("Floor");
    39.         rigidbody = GetComponent<Rigidbody> ();
    40.     }
    41.  
    42.     void FixedUpdate ()
    43.     {
    44.  
    45.         float h = Input.GetAxis ("Horizontal");
    46.         float v = Input.GetAxis ("Vertical");
    47.  
    48.         Move(h, v);
    49.  
    50.         Turning();
    51.  
    52.         rigidbody.position = new Vector3
    53.             (
    54.                 Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
    55.                 0.0f,
    56.                 Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
    57.                 );
    58.  
    59.        
    60.     }
    61.  
    62.     void Move(float h, float v)
    63.     {
    64.         movement.Set(h, 0f, v);
    65.  
    66.         movement = movement.normalized * speed * Time.deltaTime;
    67.  
    68.         rigidbody.MovePosition(transform.position + movement);
    69.     }
    70.  
    71.     void Turning()
    72.     {
    73.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    74.  
    75.         RaycastHit floorHit;
    76.  
    77.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    78.         {
    79.             Vector3 playerToMouse = floorHit.point - transform.position;
    80.  
    81.             playerToMouse.y = 0f;
    82.  
    83.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    84.  
    85.             rigidbody.MoveRotation(newRotation);
    86.         }
    87.     }
    88. }
     
    Last edited: Oct 7, 2015
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    There are a few tiny issues. For one, I think you want the inverse of floorMask (~floorMask) to pass into the raycast check.

    Also, I changed the contents of your "if" statement block to simply be:

    Code (csharp):
    1.         if (Physics.Raycast(camRay, out floorHit, camRayLength, ~floorMask))
    2.         {
    3.             transform.LookAt ( floorHit.point);
    4.         }
    Now when I did that, the act of rotating the rigidbody "drives" it funny on the map, so it is probably best to rotate a sub component of the player rather than his rigidbody. I will leave that change to you!