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. Dismiss Notice

Mouse turning

Discussion in 'Getting Started' started by kilix19980, Jun 22, 2016.

  1. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    So basicly i want my player tu turn whit mouse in 3rd person shooter like zombie army i have a simple script that alows me to move but iam not good at sripting so can someone help. Me this is my my code so far for moving
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Walk : MonoBehaviour
    5. {
    6.     public float speed = 6f;
    7.  
    8.     Vector3 movement;
    9.     Animator anim;
    10.     Rigidbody playerRigidbody;
    11.     int floorMask;
    12.     float camRayLEnght = 100f;
    13.  
    14.  
    15.     void Awake ()
    16.     {
    17.         floorMask = LayerMask.GetMask ("Floor");
    18.         anim = GetComponent<Animator> ();
    19.         playerRigidbody = GetComponent<Rigidbody> ();
    20.  
    21.     }
    22.  
    23.     void FixedUpdate ()
    24.     {
    25.         float h = Input.GetAxisRaw ("Horizontal");
    26.         float v = Input.GetAxisRaw ("Vertical");
    27.         Move (h, v);
    28.         Animating (h, v);
    29.     }
    30.  
    31.     void Move (float h, float v)
    32.     {
    33.         movement.Set (h, 0f, v);
    34.  
    35.         movement = movement.normalized * speed * Time.deltaTime;
    36.  
    37.         playerRigidbody.MovePosition (transform.position + movement);
    38.  
    39.     }
    40.  
    41.  
    42.     void Animating(float h, float v)
    43.     {
    44.         bool walking = h != 0f || v != 0f;
    45.         anim.SetBool ("IsWalking", walking);
    46.  
    47.     }
    So can someone help me i need to customize this code so i could turn whit some restrains so it wouln turn around the whole character
     
  2. macorig

    macorig

    Joined:
    Dec 27, 2010
    Posts:
    52
    I'm not sure, if this is what you meant, but you can try something like this:

    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         float h = Input.GetAxisRaw ("Horizontal");
    4.         float v = Input.GetAxisRaw ("Vertical");
    5.         Move (h, v);
    6.         Animating (h, v);
    7.  
    8.         // Create ray from mouse position
    9.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.         // Raycast, only hit floor
    11.         RaycastHit hit;
    12.         if (Physics.Raycast (ray, out hit, 50f, floorMask))
    13.         {
    14.             // Calculate direction from ray hit to player
    15.             Vector3 dir = hit.point - transform.position;
    16.             dir.y = 0;
    17.             // Rotate player to look into that direction
    18.             playerRigidbody.MoveRotation (Quaternion.LookRotation(dir));
    19.         }
    20.  
    21.     }
    Hope this helps!
     
  3. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    It helped alot but i can only turn on y axis i need x also and the speed of turning is insane when i get the camera behind the player how can i lower it
     
  4. macorig

    macorig

    Joined:
    Dec 27, 2010
    Posts:
    52
    Hi, again!
    After reading you answer and your initial post again I think I've misunderstood what exactly you were going for. Could you please provide some extra information with some more example games?
    Especially the x axis rotation has me a little confused.
     
  5. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    sorry for such a long time for this answer my pc was down for maitanace anywais i want to have a camare controler similar to a game called zombie armie trilogy i dont know how else to explain