Created
October 15, 2012 14:26
-
-
Save kawakawa/3892740 to your computer and use it in GitHub Desktop.
Unity character control
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 MarioController: MonoBehaviour { | |
float WalkSpeed=3.0f; //歩く速度 | |
float Gravity=20.0f; //重力係数 | |
float JumpSpeed=8.0f; //ジャンプ加速度 | |
private Vector3 velocity; //現在の速度 | |
// Use this for initialization | |
void Start () { | |
animation["Walk"].speed=5.0f; | |
} | |
// Update is called once per frame | |
void Update () { | |
CharacterController controller = GetComponent<CharacterController>(); | |
//地面接地中での処理 | |
if(controller.isGrounded) | |
{ | |
//キー入力から速度を求める | |
velocity=new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); | |
velocity*=WalkSpeed; | |
//ジャンプ | |
if(Input.GetButtonDown("Jump")) | |
{ | |
velocity.y=JumpSpeed; | |
animation.Play("Jump"); | |
} | |
else if(velocity.magnitude>0.5) | |
{ | |
//歩行アニメーションに切り替えつつ、進行方向に旋回 | |
animation.CrossFade("Walk",1); | |
transform.LookAt(transform.position+velocity); | |
}else | |
{ | |
animation.CrossFade("Idle",1); | |
} | |
} | |
//重力による下方向への加速 | |
velocity.y-=Gravity*Time.deltaTime; | |
//キャラの移動 | |
controller.Move(velocity*Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment