Last active
November 4, 2015 18:07
-
-
Save bdecarne/f710a8c0f137180cf747 to your computer and use it in GitHub Desktop.
Unity : ThirdPlayerCtrl.cs
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 UnityEngine; | |
using System.Collections; | |
public class Player : MonoBehaviour { | |
public Animator anim; | |
public Rigidbody rbody; | |
private float inputH; | |
private float inputV; | |
private bool run; | |
// Use this for initialization | |
void Start () { | |
anim = GetComponent<Animator>(); | |
rbody = GetComponent<Rigidbody>(); | |
run = false; | |
} | |
// Update is called once per frame | |
void Update () { | |
if(Input.GetButtonDown("Fire1")) | |
{ | |
anim.Play("attack", -1, 0f); | |
} | |
inputH = Input.GetAxis("Horizontal"); | |
inputV = Input.GetAxis("Vertical"); | |
if (Input.GetKey(KeyCode.RightShift)) | |
{ | |
run = true; | |
} | |
else | |
{ | |
run = false; | |
} | |
anim.SetFloat("inputH", inputH); | |
anim.SetFloat("inputV", inputV); | |
anim.SetBool("run", run); | |
float moveX = inputH * 20f * Time.deltaTime; | |
float moveZ = inputV * 50f * Time.deltaTime; | |
var targetVelocity = new Vector3(moveX, 0, moveZ); | |
targetVelocity = transform.TransformDirection(targetVelocity); | |
targetVelocity *= 3f; | |
// move | |
var velocity = rbody.velocity; | |
var velocityChange = (targetVelocity - velocity); | |
Debug.DrawRay(transform.position, targetVelocity * 5, Color.blue); | |
Debug.DrawRay(transform.position, velocityChange * 5, Color.magenta); | |
Debug.DrawRay(transform.position, velocity * 5, Color.green); | |
rbody.AddForce(velocityChange, ForceMode.VelocityChange); | |
// rotate | |
Vector3 rotate = new Vector3(0f, moveX, 0f); | |
transform.Rotate(rotate * 10f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment