Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace ProjectX
- {
- public class MovePlayerCharacter : StateAction
- {
- PlayerStateManager states;
- public MovePlayerCharacter(PlayerStateManager playerStateManager)
- {
- states = playerStateManager;
- }
- public override bool Execute()
- {
- states.isGrounded = OnGround();
- HandleMovement();
- #region OldMovement
- /*
- float frontY = 0;
- RaycastHit hit;
- Vector3 targetVelocity = Vector3.zero;
- if (states.lockOn)
- {
- targetVelocity = states.mTransform.forward * states.vertical * states.movementSpeed;
- targetVelocity += states.mTransform.right * states.horizontal * states.movementSpeed;
- }
- else
- {
- targetVelocity = states.mTransform.forward * states.moveAmount * states.movementSpeed;
- }
- Vector3 origin = states.mTransform.position + (targetVelocity.normalized * states.frontRayOffset);
- origin.y += .5f;
- Debug.DrawRay(origin, -Vector3.up, Color.red, .01f, false);
- if (Physics.Raycast(origin, -Vector3.up, out hit, 1, states.ignoreForGroundCheck))
- {
- float y = hit.point.y;
- frontY = y - states.mTransform.position.y;
- }
- Vector3 currentVelocity = states.rigidbody.velocity;
- //if (states.isLockingOn)
- //{
- // targetVelocity = states.rotateDirection * states.moveAmount * movementSpeed;
- //}
- if (states.isGrounded)
- {
- float moveAmount = states.moveAmount;
- if (moveAmount > 0.1f)
- {
- states.rigidbody.isKinematic = false;
- states.rigidbody.drag = 0;
- if (Mathf.Abs(frontY) > 0.02f)
- {
- targetVelocity.y = ((frontY > 0) ? frontY + 0.2f : frontY - 0.2f) * states.movementSpeed;
- }
- }
- else
- {
- float abs = Mathf.Abs(frontY);
- if (abs > 0.02f)
- {
- states.rigidbody.isKinematic = true;
- targetVelocity.y = 0;
- states.rigidbody.drag = 4;
- }
- }
- HandleRotation();
- }
- else
- {
- //states.collider.height = colStartHeight;
- states.rigidbody.isKinematic = false;
- states.rigidbody.drag = 0;
- targetVelocity.y = currentVelocity.y;
- }
- HandleAnimations();
- Debug.DrawRay((states.mTransform.position + Vector3.up * .2f), targetVelocity, Color.green, 0.01f, false);
- states.rigidbody.velocity = targetVelocity;
- // Debug.Log(targetVelocity);
- //states.rigidbody.velocity = Vector3.Lerp(currentVelocity, targetVelocity, states.delta * states.adaptSpeed);
- */
- #endregion OldMovement
- return false;
- }
- void HandleMovement()
- {
- Vector3 axisY = states.transform.forward;
- Vector3 axisX = states.transform.right;
- Vector3 targetVelocity = Vector3.zero;
- if (states.isGrounded)
- {
- if (states.vertical != 0 || states.horizontal != 0)
- {
- if (states.lockOn)
- {
- axisY = states.vertical * states.transform.forward;
- axisX = states.horizontal * states.transform.right;
- states.rigidbody.isKinematic = false;
- states.rigidbody.drag = 0;
- }
- else
- {
- axisY = states.vertical * states.transform.forward;
- axisX = states.horizontal * states.transform.right;
- states.rigidbody.isKinematic = false;
- states.rigidbody.drag = 0;
- }
- }
- else
- {
- states.rigidbody.isKinematic = true;
- states.rigidbody.drag = 4;
- }
- HandleRotation();
- HandleTargetSpeed();
- }
- else
- {
- //states.collider.height = colStartHeight;
- states.rigidbody.isKinematic = false;
- states.rigidbody.drag = 0;
- }
- states.rootMovement = (axisY + axisX).normalized;
- targetVelocity = states.rootMovement * states.movementSpeed * (states.targetSpeed);
- HandleAnimations();
- Debug.DrawRay((states.mTransform.position + Vector3.up * .2f), targetVelocity, Color.green, 0.01f, false);
- states.rigidbody.velocity = targetVelocity;
- }
- void HandleTargetSpeed()
- {
- if (states.isGrounded)
- {
- if (states.isRunning && states.vertical != 0 && states.vertical > 0)
- {
- if (states.targetSpeed < 2)
- {
- states.targetSpeed = states.targetSpeed + 2.5f * Time.deltaTime;
- }
- else
- {
- states.targetSpeed = 2;
- }
- }
- else
- {
- if (states.targetSpeed > 1 && states.vertical > 0)
- {
- states.targetSpeed = states.targetSpeed - 2f * Time.deltaTime;
- }
- else
- {
- states.targetSpeed = 1;
- }
- }
- }
- }
- void HandleRotation()
- {
- Vector3 targetDir = Vector3.zero;
- float moveOverride = states.moveAmount;
- if (states.lockOn)
- {
- targetDir = states.target.position - states.mTransform.position;
- moveOverride = 1;
- }
- else
- {
- targetDir = states.camera.transform.forward;
- }
- targetDir.Normalize();
- targetDir.y = 0;
- if (targetDir == Vector3.zero)
- targetDir = states.mTransform.forward;
- Quaternion tr = Quaternion.LookRotation(targetDir);
- Quaternion targetRotation = Quaternion.Slerp(
- states.mTransform.rotation, tr,
- states.delta * moveOverride * states.rotationSpeed);
- states.mTransform.rotation = targetRotation;
- #region OldStuff
- /*
- Vector3 targetDir = Vector3.zero;
- float moveOverride = states.moveAmount;
- if (states.lockOn)
- {
- targetDir = states.target.position - states.mTransform.position;
- moveOverride = 1;
- }
- else
- {
- float h = states.horizontal;
- float v = states.vertical;
- targetDir = states.camera.transform.forward * v;
- targetDir += states.camera.transform.right * h;
- }
- targetDir.Normalize();
- targetDir.y = 0;
- if (targetDir == Vector3.zero)
- targetDir = states.mTransform.forward;
- Quaternion tr = Quaternion.LookRotation(targetDir);
- Quaternion targetRotation = Quaternion.Slerp(
- states.mTransform.rotation, tr,
- states.delta * moveOverride * states.rotationSpeed);
- states.mTransform.rotation = targetRotation;
- */
- #endregion OldStuff
- }
- void HandleAnimations()
- {
- if (states.isGrounded)
- {
- if (states.lockOn)
- {
- states.anim.SetFloat(StaticStrings.vertical, states.vertical);
- states.anim.SetFloat(StaticStrings.horizontal, states.horizontal * states.targetSpeed);
- }
- else
- {
- states.anim.SetFloat(StaticStrings.vertical, states.vertical * states.targetSpeed);
- states.anim.SetFloat(StaticStrings.horizontal, states.horizontal);
- }
- }
- else
- {
- }
- }
- bool OnGround()
- {
- bool retVal = false;
- Vector3 origin = states.mTransform.position;
- origin.y += .7f;
- Vector3 dir = -Vector3.up;
- float dis = 1.4f;
- RaycastHit hit;
- Debug.DrawRay(origin, -Vector3.up * dis, Color.red);
- //Debug.DrawRay(origin, dir * dis);
- if (Physics.Raycast(origin, dir, out hit, dis, states.ignoreForGroundCheck))
- {
- retVal = true;
- Vector3 targetPosition = hit.point;
- states.mTransform.position = targetPosition;
- }
- return retVal;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement