Created
April 7, 2017 00:11
-
-
Save nhaskins/d7895c0f3472e70979d7935cb80cc16d to your computer and use it in GitHub Desktop.
2.5D dino controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DinoController : MonoBehaviour { | |
public float walkSpeed = 5f; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
//capture input | |
float h = Input.GetAxis("Horizontal"); | |
float v = Input.GetAxis("Vertical"); | |
float walkDelta = walkSpeed * Time.deltaTime; | |
//translate the char based on input | |
Vector3 v3h = new Vector3(-v * walkDelta, 0, 0); | |
Vector3 v3v = new Vector3(0, 0, h * walkDelta); | |
transform.Translate(v3h, Space.World); | |
transform.Translate(v3v, Space.World); | |
//update char animation | |
bool walkState = !(v == h); | |
GetComponent<Animator>().SetBool("IsWalking", walkState); | |
//rotate char based on input | |
if(h != 0){ | |
//control is pressed, update direction | |
float direction = h < 0 ? 0 : -180f; | |
transform.localRotation = Quaternion.Euler(0, direction, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment