Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| wiki:character_setup [2022/08/01 17:29] – admin | wiki:character_setup [2022/08/05 12:03] (current) – [Scripts] admin | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| Instead of we use build in unity animation manager, we use Animancer to animate directly using script, This is easier and faster! | Instead of we use build in unity animation manager, we use Animancer to animate directly using script, This is easier and faster! | ||
| </ | </ | ||
| + | |||
| + | ====== Introduction Easy Character Movement ====== | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | <file | ECM? > | ||
| + | By using ECM framework we can use their framework and tweak it as we wanted to | ||
| + | </ | ||
| + | |||
| + | ====== Scripts ====== | ||
| + | |||
| + | <code CSharp | MyCharacterController derived by BaseCharacterController, | ||
| + | using ECM.Common; | ||
| + | using ECM.Controllers; | ||
| + | using UnityEngine; | ||
| + | |||
| + | public sealed class MyCharacterController : BaseCharacterController | ||
| + | { | ||
| + | public Transform playerCamera; | ||
| + | |||
| + | public float customSpeedWalk = 3f; | ||
| + | public float customSpeedRun = 10f; | ||
| + | |||
| + | public void makeWalk() | ||
| + | { | ||
| + | speed = customSpeedWalk; | ||
| + | } | ||
| + | |||
| + | public void makeRun() | ||
| + | { | ||
| + | speed = customSpeedRun; | ||
| + | } | ||
| + | |||
| + | protected override void HandleInput() | ||
| + | { | ||
| + | // Handle your custom input here... | ||
| + | moveDirection = new UnityEngine.Vector3 | ||
| + | { | ||
| + | x = Input.GetAxisRaw(" | ||
| + | y = 0.0f, | ||
| + | z = Input.GetAxisRaw(" | ||
| + | }; | ||
| + | |||
| + | if(!isJumping) jump = Input.GetButton(" | ||
| + | |||
| + | //walk = Input.GetButton(" | ||
| + | |||
| + | // Transform moveDirection vector to be relative to camera view direction | ||
| + | moveDirection = moveDirection.relativeTo(playerCamera); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | ExploreCameraController, | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using DG.Tweening; | ||
| + | using Sirenix.OdinInspector; | ||
| + | |||
| + | public class ExploreCameraController : MonoBehaviour | ||
| + | { | ||
| + | public Transform mainCamera; | ||
| + | |||
| + | public float xVal = 0; | ||
| + | private float turnSpeed = 4; | ||
| + | private Vector3 offsetX; | ||
| + | |||
| + | public float height = 15f; | ||
| + | public float distance = 12f; | ||
| + | |||
| + | public float lookAtSensitivity = 0.3f; | ||
| + | public float rotateSensitivity = 0.3f; | ||
| + | public float mouseSensitive = 5; | ||
| + | |||
| + | public float turnSpeedSensitivity = 0.05f; | ||
| + | |||
| + | void Awake() | ||
| + | { | ||
| + | offsetX = new Vector3(0, height, distance); | ||
| + | transform.DOMove(new Vector3((mainCamera.position + offsetX).x, height, (mainCamera.position + offsetX).z), | ||
| + | } | ||
| + | |||
| + | [Button] | ||
| + | void updateHeightDistance() | ||
| + | { | ||
| + | offsetX = new Vector3(0, height, distance); | ||
| + | } | ||
| + | |||
| + | //public float zVal = 0; | ||
| + | |||
| + | // Update is called once per frame | ||
| + | void LateUpdate() | ||
| + | { | ||
| + | | ||
| + | this.transform.DOLookAt(mainCamera.position, | ||
| + | // | ||
| + | |||
| + | if (Input.GetKey(KeyCode.Mouse0)) | ||
| + | { | ||
| + | offsetX = Quaternion.AngleAxis(Input.GetAxis(" | ||
| + | } | ||
| + | if (Input.GetKey(KeyCode.E)) | ||
| + | { | ||
| + | offsetX = Quaternion.AngleAxis(1 * (turnSpeed * turnSpeedSensitivity * Time.deltaTime), | ||
| + | } | ||
| + | if (Input.GetKey(KeyCode.Q)) | ||
| + | { | ||
| + | offsetX = Quaternion.AngleAxis(-1 * (turnSpeed * turnSpeedSensitivity * Time.deltaTime), | ||
| + | } | ||
| + | |||
| + | // | ||
| + | transform.DOMove(new Vector3((mainCamera.position + offsetX).x, height, (mainCamera.position + offsetX).z), | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | We use script to assign animation into our character.> | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using Animancer; | ||
| + | using Animancer.Examples; | ||
| + | using ECM.Controllers; | ||
| + | |||
| + | public class CharacterAnimationController : MonoBehaviour | ||
| + | { | ||
| + | |||
| + | [SerializeField] private AnimancerComponent _Animancer; | ||
| + | [SerializeField] private ClipTransition _Idle; | ||
| + | [SerializeField] private ClipTransition _Move; | ||
| + | [SerializeField] private ClipTransition _Run; | ||
| + | [SerializeField] private ClipTransition _Action; | ||
| + | |||
| + | private MyCharacterController myCharacterController; | ||
| + | |||
| + | private void Start() | ||
| + | { | ||
| + | myCharacterController = FindObjectOfType< | ||
| + | } | ||
| + | |||
| + | private void Update() | ||
| + | { | ||
| + | |||
| + | if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) | ||
| + | { | ||
| + | _Animancer.Play(_Move); | ||
| + | |||
| + | myCharacterController.makeWalk(); | ||
| + | |||
| + | if (Input.GetKey(KeyCode.LeftShift)) | ||
| + | { | ||
| + | _Animancer.Play(_Run); | ||
| + | myCharacterController.makeRun(); | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | _Animancer.Play(_Idle); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||