Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gkagm2
Created June 18, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkagm2/63e9d681809214eaa4ea48b110879464 to your computer and use it in GitHub Desktop.
Save gkagm2/63e9d681809214eaa4ea48b110879464 to your computer and use it in GitHub Desktop.
aniamtor에서 animation의 length 설정,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityChanController06 : MonoBehaviour {
// parameters
public int speed;
// animator hash
int dirForwardHash = Animator.StringToHash("Dir_Forward");
int moveHash = Animator.StringToHash("Move");
float timeOfAnimation;
// references
Animator anim;
CharacterController characterController;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
timeOfAnimation = GetComponent<Animator>().runtimeAnimatorController.animationClips[0].length;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Q))
{
anim.SetTrigger("Attack");
if (anim.GetCurrentAnimatorStateInfo(1).IsName("Punch") || anim.GetCurrentAnimatorStateInfo(1).normalizedTime == 0.5f)
{
anim.Play("Punch", 1, 0.5f);
//anim.SetFloat("normalizedTime", 0.5f);
}
}
else
{
anim.ResetTrigger("Attack");
if (anim.GetCurrentAnimatorStateInfo(1).IsName("Punch"))
{
//anim.SetFloat("spsped", 1f);
}
}
float dir_f = Input.GetAxis("Vertical"); // f : forward
Debug.Log("dir_f : " + dir_f);
if (dir_f != 0) // move
{
anim.SetBool(moveHash, true);
anim.SetFloat(dirForwardHash, dir_f);
}
else
{ // just stand
anim.SetBool(moveHash, false);
}
// Movement
characterController.Move(new Vector3(0, 0, dir_f * speed * Time.deltaTime));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment