unity简单人物控制
2017-10-19 09:40
429 查看
public class PlayerMove : MonoBehaviour
{
private CharacterController cc;
[SerializeField]
private float speed; //向前走的速度
[SerializeField]
private float jumpSpeedValue; //向上跳的速度值
private Animator ani;
private Vector3 JumpSpeed; //向上跳的速度
private bool isJumping = false; ///是否正在跳跃
private float gravity = 3; //重力大小
private ETCJoystick joyStick;
private ETCButton jumpBtn;
private void Awake()
{
speed = 4;
JumpSpeed = Vector3.zero;
jumpSpeedValue = 2;
isJumping = false;
gravity = 3;
cc = this.GetComponent<CharacterController>();
ani = this.GetComponent<Animator>();
}
void Start()
{
joyStick = ETCInput.GetControlJoystick("moveStick");
jumpBtn = ETCInput.GetControlButton("jumpBtn");
jumpBtn.onDown.AddListener(jumpBtnDown);
// joyStick.onMove.AddListener(onMove);
}
private void jumpBtnDown()
{
if (cc.isGrounded) //判断人物是否已经落到地面。
{
//JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。
//isJumping = true;
ani.SetTrigger("jump");
}
}
//private void onMove(Vector2 v)
//{
// print(string.Format("x:{0} y:{1} ",v.x,v.y));
//}
void Update()
{
float h = joyStick.axisX.axisValue;
float v = joyStick.axisY.axisValue;
Vector3 sp = Vector3.zero;
if (h != 0 || v != 0)
{
ani.SetBool("run", true);
ani.SetFloat("runSpeed", speed / 2); ///同步动画播放的速度。
///速度和方向
Vector3 dir = new Vector3(h, 0, v);
dir = Camera.main.transform.TransformDirection(dir);
dir.y = 0;
dir.Normalize(); //长度为1.
sp = dir * 4;
//转身
this.transform.LookAt(this.transform.position + sp);
}
else
{
ani.SetBool("run", false);
}
///得到当前播放个的动画信息。
///正在播放run动画活着在空中时候可以发生位移
AnimatorStateInfo stateinfo = ani.GetCurrentAnimatorStateInfo(0);
if (stateinfo.IsName("Run") || (stateinfo.IsName("Jump") && !cc.isGrounded))
{
cc.SimpleMove(sp);
}
//if (Input.GetButtonDown("Jump"))
//{
// if (cc.isGrounded) //判断人物是否已经落到地面。
// {
// JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。
// isJumping = true;
// ani.SetTrigger("jump");
// }
//}
//if (isJumping) //正在跳跃
//{
// //向上跳
// cc.Move(JumpSpeed * Time.deltaTime);
// //加上重力
// JumpSpeed += Time.deltaTime * Vector3.down * gravity;
// if(cc.isGrounded)
// {
// isJumping = false;
// }
//}
}
}
{
private CharacterController cc;
[SerializeField]
private float speed; //向前走的速度
[SerializeField]
private float jumpSpeedValue; //向上跳的速度值
private Animator ani;
private Vector3 JumpSpeed; //向上跳的速度
private bool isJumping = false; ///是否正在跳跃
private float gravity = 3; //重力大小
private ETCJoystick joyStick;
private ETCButton jumpBtn;
private void Awake()
{
speed = 4;
JumpSpeed = Vector3.zero;
jumpSpeedValue = 2;
isJumping = false;
gravity = 3;
cc = this.GetComponent<CharacterController>();
ani = this.GetComponent<Animator>();
}
void Start()
{
joyStick = ETCInput.GetControlJoystick("moveStick");
jumpBtn = ETCInput.GetControlButton("jumpBtn");
jumpBtn.onDown.AddListener(jumpBtnDown);
// joyStick.onMove.AddListener(onMove);
}
private void jumpBtnDown()
{
if (cc.isGrounded) //判断人物是否已经落到地面。
{
//JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。
//isJumping = true;
ani.SetTrigger("jump");
}
}
//private void onMove(Vector2 v)
//{
// print(string.Format("x:{0} y:{1} ",v.x,v.y));
//}
void Update()
{
float h = joyStick.axisX.axisValue;
float v = joyStick.axisY.axisValue;
Vector3 sp = Vector3.zero;
if (h != 0 || v != 0)
{
ani.SetBool("run", true);
ani.SetFloat("runSpeed", speed / 2); ///同步动画播放的速度。
///速度和方向
Vector3 dir = new Vector3(h, 0, v);
dir = Camera.main.transform.TransformDirection(dir);
dir.y = 0;
dir.Normalize(); //长度为1.
sp = dir * 4;
//转身
this.transform.LookAt(this.transform.position + sp);
}
else
{
ani.SetBool("run", false);
}
///得到当前播放个的动画信息。
///正在播放run动画活着在空中时候可以发生位移
AnimatorStateInfo stateinfo = ani.GetCurrentAnimatorStateInfo(0);
if (stateinfo.IsName("Run") || (stateinfo.IsName("Jump") && !cc.isGrounded))
{
cc.SimpleMove(sp);
}
//if (Input.GetButtonDown("Jump"))
//{
// if (cc.isGrounded) //判断人物是否已经落到地面。
// {
// JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。
// isJumping = true;
// ani.SetTrigger("jump");
// }
//}
//if (isJumping) //正在跳跃
//{
// //向上跳
// cc.Move(JumpSpeed * Time.deltaTime);
// //加上重力
// JumpSpeed += Time.deltaTime * Vector3.down * gravity;
// if(cc.isGrounded)
// {
// isJumping = false;
// }
//}
}
}
相关文章推荐
- Unity模拟龙之谷人物行走简单控制
- Unity模拟龙之谷人物行走简单控制
- Unity模拟龙之谷人物行走简单控制
- unity 场景NPC触发漫游和间隔时间自动显示对话在人物上方的简单脚本!
- unity中简单的角色移动控制
- unity。。。完美简单解决靠鼠标在屏幕上的移动来控制相机水平,垂直旋转的c#脚本
- unity 手游 3D人物的移动控制
- 【Unity 3D学习】键盘控制人物在场景中移动
- Unity模拟龙之谷人物控制(二)
- Unity代码控制TweenRotation旋转方向(简单记录)
- KING_UNITY学习之鼠标点击地面控制人物移动
- 【unity学习笔记】unity控制人物跳跃,处理CharacterController的isGrounded总是返回false的问题
- 【Unity 3D学习】鼠标点击控制人物移动到目标位置
- Unity 通过点击屏幕控制人物移动
- Unity 3D 一个简单的角色控制脚本
- 《unity》实现对游戏人物的控制
- Unity3D游戏开发之史上最简单的鼠标点击控制人物走动实现
- [Unity动画]给人物模型添加简单的动画
- Unity实现点击地面控制人物移动
- 【Unity3D自学记录】简单的鼠标点击控制人物走动