hello, we have script in c# : Code (csharp): using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public Transform cameraHandle; public Transform troll; public Animator animator; public Controller controller; public GUIStyle labelStyle; private const int barrelsToFound = 5; private const float walkSpeed = 2.0f; private const float runSpeed = 12.0f; private readonly Vector2 gravity = new Vector3(0, -9, 0); private int foundBarrels; private CharacterController characterController; // Use this for initialization void Start () { characterController = GetComponent<CharacterController>(); } // Update is called once per frame void Update () { float speed = 0; if (Input.GetKey(KeyCode.W)) { //move hero speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed; float angle = cameraHandle.eulerAngles.y * Mathf.Deg2Rad; Vector3 direction = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)); characterController.Move(direction * speed * Time.deltaTime); //turn around troll.eulerAngles = new Vector3(troll.eulerAngles.x, cameraHandle.eulerAngles.y, troll.eulerAngles.z); } if (!characterController.isGrounded) { characterController.Move(gravity * Time.deltaTime); } animator.SetFloat("speed", speed); } i wonder how does " Code (csharp): 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 ??
its a short handed way of writing an if statement. Its the same as Code (csharp): if( Input.GetKey( KeyCode.LeftShift ) ) { speed = runSpeed; } else { speed = walkSpeed; } http://msdn.microsoft.com/en-us/library/ty67wk28.aspx
thanks a lot, i have additional question about that script Code (csharp): 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?
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.)