203 lines
5.4 KiB
C#
203 lines
5.4 KiB
C#
//
|
|
// Mecanimのアニメーションデータが、原点で移動しない場合の Rigidbody付きコントローラ
|
|
// サンプル
|
|
// 2014/03/13 N.Kobyasahi
|
|
//
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
[RequireComponent(typeof (Animator))]
|
|
[RequireComponent(typeof (CapsuleCollider))]
|
|
[RequireComponent(typeof (Rigidbody))]
|
|
|
|
public class UnityChanControlScriptWithRgidBody : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static UnityChanControlScriptWithRgidBody instance;
|
|
public float animSpeed = 1.5f;
|
|
public float lookSmoother = 3.0f;
|
|
public bool useCurves = true;
|
|
public Text txt;
|
|
public float useCurvesHeight = 0.5f;
|
|
|
|
|
|
// 前進速度
|
|
public float forwardSpeed = 7.0f;
|
|
// 後退速度
|
|
public float backwardSpeed = 2.0f;
|
|
// 旋回速度
|
|
public float rotateSpeed = 2.0f;
|
|
|
|
public float jumpPower = 3.0f;
|
|
|
|
private CapsuleCollider col;
|
|
private Rigidbody rb;
|
|
|
|
private Vector3 velocity;
|
|
|
|
private float orgColHight;
|
|
private Vector3 orgVectColCenter;
|
|
|
|
private Animator anim;
|
|
private AnimatorStateInfo currentBaseState;
|
|
|
|
private GameObject cameraObject;
|
|
|
|
static int idleState = Animator.StringToHash("Base Layer.Idle");
|
|
static int locoState = Animator.StringToHash("Base Layer.Locomotion");
|
|
static int jumpState = Animator.StringToHash("Base Layer.Jump");
|
|
static int restState = Animator.StringToHash("Base Layer.Rest");
|
|
// public GameObject anniuobj;
|
|
public int ybindex;
|
|
private float oldtime;
|
|
|
|
|
|
[SerializeField]
|
|
[Tooltip("鼠标移动灵敏度")]
|
|
public float sensitivity = 2;
|
|
[Tooltip("移动中平滑")]
|
|
public float smoothing = 1.5f;
|
|
Vector2 v;
|
|
Vector2 frameVelocity;
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
|
|
// 初期化
|
|
void Start ()
|
|
{
|
|
|
|
anim = GetComponent<Animator>();
|
|
|
|
col = GetComponent<CapsuleCollider>();
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
cameraObject = GameObject.FindWithTag("MainCamera");
|
|
|
|
orgColHight = col.height;
|
|
orgVectColCenter = col.center;
|
|
ybindex = 2;
|
|
oldtime = Time.time;
|
|
}
|
|
Quaternion camaeraXAngle;//获取鼠标X轴偏移量当摄像机旋转角度
|
|
Quaternion camaeraYAngle;//获取鼠标Y轴偏移量当摄像机旋转角度
|
|
|
|
|
|
void FixedUpdate()
|
|
{
|
|
float h = 0;
|
|
h = Input.GetAxis("Horizontal");
|
|
float v = Input.GetAxis("Vertical");
|
|
anim.SetFloat("Speed", v);
|
|
anim.SetFloat("Direction", h);
|
|
|
|
|
|
anim.speed = animSpeed;
|
|
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
|
|
rb.useGravity = true;
|
|
|
|
velocity = new Vector3(0, 0, v);
|
|
|
|
velocity = transform.TransformDirection(velocity);
|
|
if (v > 0.1)
|
|
{
|
|
velocity *= forwardSpeed;
|
|
}
|
|
else if (v < -0.1)
|
|
{
|
|
velocity *= backwardSpeed;
|
|
}
|
|
transform.localPosition += velocity * Time.fixedDeltaTime;
|
|
if (Input.GetKey(KeyCode.Mouse1))
|
|
{
|
|
MouseRotate();
|
|
}
|
|
if (currentBaseState.nameHash == locoState)
|
|
{
|
|
if (useCurves)
|
|
{
|
|
resetCollider();
|
|
}
|
|
}
|
|
else if (currentBaseState.nameHash == jumpState)
|
|
{
|
|
cameraObject.SendMessage("setCameraPositionJumpView");
|
|
|
|
if (!anim.IsInTransition(0))
|
|
{
|
|
|
|
|
|
if (useCurves)
|
|
{
|
|
|
|
float jumpHeight = anim.GetFloat("JumpHeight");
|
|
float gravityControl = anim.GetFloat("GravityControl");
|
|
if (gravityControl > 0)
|
|
rb.useGravity = false;
|
|
|
|
|
|
Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
|
|
RaycastHit hitInfo = new RaycastHit();
|
|
if (Physics.Raycast(ray, out hitInfo))
|
|
{
|
|
if (hitInfo.distance > useCurvesHeight)
|
|
{
|
|
col.height = orgColHight - jumpHeight;
|
|
float adjCenterY = orgVectColCenter.y + jumpHeight;
|
|
col.center = new Vector3(0, adjCenterY, 0);
|
|
}
|
|
else
|
|
{
|
|
resetCollider();
|
|
}
|
|
}
|
|
}
|
|
anim.SetBool("Jump", false);
|
|
}
|
|
}
|
|
else if (currentBaseState.nameHash == idleState)
|
|
{
|
|
if (useCurves)
|
|
{
|
|
resetCollider();
|
|
}
|
|
if (Input.GetButtonDown("Jump"))
|
|
{
|
|
anim.SetBool("Rest", true);
|
|
}
|
|
}
|
|
else if (currentBaseState.nameHash == restState)
|
|
{
|
|
if (!anim.IsInTransition(0))
|
|
{
|
|
anim.SetBool("Rest", false);
|
|
}
|
|
}
|
|
}
|
|
void resetCollider()
|
|
{
|
|
col.height = orgColHight;
|
|
col.center = orgVectColCenter;
|
|
}
|
|
|
|
bool isInit;
|
|
void MouseRotate()
|
|
{
|
|
Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
|
|
Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
|
|
frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
|
|
v += frameVelocity;
|
|
v.y = Mathf.Clamp(v.y, -60, 60);
|
|
transform.localRotation = Quaternion.AngleAxis(v.x-90, Vector3.up);
|
|
}
|
|
|
|
}
|