1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Animator))]
[RequireComponent(typeof (CapsuleCollider))]
[RequireComponent(typeof (Rigidbody))]
public class UnityChanControlScriptWithRgidBody : MonoBehaviour
{
public float animSpeed = 1.5f;
public float lookSmoother = 3.0f;
public bool useCurves = true;
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");
void Start ()
{
anim = GetComponent<Animator>();
col = GetComponent<CapsuleCollider>();
rb = GetComponent<Rigidbody>();
cameraObject = GameObject.FindWithTag("MainCamera");
orgColHight = col.height;
orgVectColCenter = col.center;
}
void FixedUpdate ()
{
float 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;
}
if (Input.GetButtonDown("Jump")) {
if (currentBaseState.nameHash == locoState){
if(!anim.IsInTransition(0))
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
anim.SetBool("Jump", true);
}
}
}
transform.localPosition += velocity * Time.fixedDeltaTime;
transform.Rotate(0, h * rotateSpeed, 0);
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;
}
} |
Partager