Search Unity

How does this work

Discussion in 'Scripting' started by yami7, May 14, 2014.

  1. yami7

    yami7

    Joined:
    May 8, 2014
    Posts:
    5
    hello, we have script in c# :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     public Transform cameraHandle;
    8.     public Transform troll;
    9.     public Animator animator;
    10.     public Controller controller;
    11.     public GUIStyle labelStyle;
    12.  
    13.     private const int barrelsToFound = 5;
    14.     private const float walkSpeed = 2.0f;
    15.     private const float runSpeed = 12.0f;
    16.     private readonly Vector2 gravity = new Vector3(0, -9, 0);
    17.  
    18.     private int foundBarrels;
    19.     private CharacterController characterController;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         characterController = GetComponent<CharacterController>();
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update ()
    28.     {
    29.         float speed = 0;
    30.  
    31.         if (Input.GetKey(KeyCode.W))
    32.         {
    33.             //move hero
    34.             speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
    35.             float angle = cameraHandle.eulerAngles.y * Mathf.Deg2Rad;
    36.             Vector3 direction = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle));
    37.             characterController.Move(direction * speed * Time.deltaTime);
    38.  
    39.             //turn around
    40.             troll.eulerAngles = new Vector3(troll.eulerAngles.x, cameraHandle.eulerAngles.y, troll.eulerAngles.z);
    41.  
    42.         }
    43.  
    44.         if (!characterController.isGrounded)
    45.         {
    46.             characterController.Move(gravity * Time.deltaTime);
    47.         }
    48.  
    49.         animator.SetFloat("speed", speed);
    50.     }
    51.  
    52.  

    i wonder how does "
    Code (csharp):
    1.  speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
    " line work ??
    speed = 0, when 'w' is pressed down speed= 2.0f,
    and when 'w' and 'left shift' is pressed down speed = 12.0f ?? How does it work and why so ??
     
  2. Deleted User

    Deleted User

    Guest

  3. yami7

    yami7

    Joined:
    May 8, 2014
    Posts:
    5
    thanks a lot, i have additional question about that script
    Code (csharp):
    1.   Vector3 direction = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle));
    I heard that it creates a unit vector. What it does is that our hero moves in correct direction (forward). I'm not sure how does it work??
    What does sin/cosine excatly do here? And why is it Unit vector?
     
    Last edited: May 15, 2014
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Sin and Cos are basically coordinates around a circle given angle. so your number. Look those up here:

    http://www.regentsprep.org/Regents/math/algtrig/ATT7/sincostan.htm

    As you move your number up both of them, you will see that he position changes based on some curvey line. Well, sin is X and Cos is Y, so you can get x and y values from them and translate a circle to a grid. This gird is Unit length... as explained below.

    Unit Vector.

    Unit refers to the number 1 and vector is a direction. So Unit Vector literally means a direction that is exactly 1 in distance.

    So your statement actually looks like this:

    direction = vector (x, 0, y) where x and y are based off of a SinCos angle because SinCos is always unit length, you simply get a unit vector out of it.

    Vectors represent 3 different concepts which make them the best and most versatile things to use.

    Position
    Direction
    Length

    Any vector can be unit length, the process is called Normalization. "direction.normalized" does this. This means that it sets whatever that vector is to 1 unit length.

    Why is 1 unit important? Simple, if you have a speed of 5, but you want to figure out how much of that is supposed to be moved this frame, you want a unit vector to multiply against.

    direction.normalized * speed * Time.deltaTime

    is your distance moved from last frame to this frame. (Time.deltaTime is the amout of time that has passed since the start of last frame to the start of this frame.)