unity3D 控制物体移动和动画播放
程序员文章站
2024-01-18 19:18:58
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public float speed = 0.1F;
public float rotatespeed = 3.0f;
private Transform tran;
private Animator m_animtor;
private float h;
private float v;
// Use this for initialization
void Start()
{
tran = gameObject.GetComponent();
m_animtor = GetComponent();
}
// Update is called once per frame
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
transform.Rotate(0, h * rotatespeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * v;
if (Mathf.Abs(v) > 0.1)
{
if (Input.GetKey(KeyCode.W))
{
//tran.Translate(Vector3.forward * 0.1f);
//m_animtor.SetBool("walkone", true);8
if (Input.GetKey(KeyCode.LeftShift))
{
tran.Translate(Vector3.forward * 0.5f);
m_animtor.SetBool("runone", true);
}
}
else if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.W))
{
m_animtor.SetBool("runone", false);
}
//else if (Input.GetKeyUp(KeyCode.W))
//{
// m_animtor.SetBool("walkone", false);
//}
}
if (Input.GetKey(KeyCode.K))
{
m_animtor.SetBool("attack", true);
}
else if (Input.GetKeyUp(KeyCode.K))
{
m_animtor.SetBool("attack", false);
}
}
}
上一篇: java 枚举类、实现接口