case1
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class CharacterMovement : MonoBehaviour
|
||||
{
|
||||
public bool useCharacterForward = false;
|
||||
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
|
||||
public KeyCode sprintKeyboard = KeyCode.Space;
|
||||
|
||||
private float speed = 0f;
|
||||
private float direction = 0f;
|
||||
private bool isSprinting = false;
|
||||
private Rigidbody rb;
|
||||
private Animator anim;
|
||||
private Vector2 input;
|
||||
private Camera mainCamera;
|
||||
private float velocity;
|
||||
|
||||
void Start()
|
||||
{
|
||||
anim = GetComponent<Animator>();
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
anim.updateMode = AnimatorUpdateMode.Fixed;
|
||||
anim.animatePhysics = true;
|
||||
#else
|
||||
anim.updateMode = AnimatorUpdateMode.AnimatePhysics;
|
||||
#endif
|
||||
anim.applyRootMotion = true;
|
||||
mainCamera = Camera.main;
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
rb.constraints &= ~RigidbodyConstraints.FreezeRotationY;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
input.x = Input.GetAxis("Horizontal");
|
||||
input.y = Input.GetAxis("Vertical");
|
||||
|
||||
// set speed to both vertical and horizontal inputs
|
||||
if (useCharacterForward)
|
||||
speed = Mathf.Abs(input.x) + input.y;
|
||||
else
|
||||
speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
|
||||
|
||||
speed = Mathf.Clamp(speed, 0f, 1f);
|
||||
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
|
||||
|
||||
if (input.y < 0f && useCharacterForward)
|
||||
direction = input.y;
|
||||
else
|
||||
direction = 0f;
|
||||
|
||||
isSprinting = (Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f;
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Interact with Rigidbody only in FixedUpdate
|
||||
void FixedUpdate()
|
||||
{
|
||||
anim.SetFloat("Speed", speed);
|
||||
anim.SetFloat("Direction", direction);
|
||||
anim.SetBool("isSprinting", isSprinting);
|
||||
|
||||
// Update target direction relative to the camera view or player forward
|
||||
var tr = useCharacterForward ? transform : mainCamera.transform;
|
||||
var right = tr.right;
|
||||
var forward = tr.forward;
|
||||
forward.y = 0;
|
||||
var targetDir = input.x * right + (useCharacterForward ? Mathf.Abs(input.y) : input.y) * forward;
|
||||
|
||||
if (input == Vector2.zero || targetDir.magnitude < 0.1f)
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
else
|
||||
{
|
||||
targetDir = targetDir.normalized;
|
||||
var currentDir = rb.rotation * Vector3.forward;
|
||||
|
||||
// Rigidbody.MoveRotation breaks interpolation for non-kinematic rigidbodies, so don't use it
|
||||
var angle = Vector3.SignedAngle(currentDir, targetDir, Vector3.up) * Mathf.Deg2Rad / Time.fixedDeltaTime;
|
||||
rb.angularVelocity = Vector3.up * angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5281785fbe32be4488391fbe4acda0d3
|
||||
timeCreated: 1507745949
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class CharacterMovement2D : MonoBehaviour
|
||||
{
|
||||
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
|
||||
public KeyCode jumpJoystick = KeyCode.JoystickButton0;
|
||||
public KeyCode sprintKeyboard = KeyCode.LeftShift;
|
||||
public KeyCode jumpKeyboard = KeyCode.Space;
|
||||
public float jumpVelocity = 7f;
|
||||
public float groundTolerance = 0.2f;
|
||||
public bool checkGroundForJump = true;
|
||||
|
||||
float speed = 0f;
|
||||
bool isSprinting = false;
|
||||
Animator anim;
|
||||
Vector2 input;
|
||||
float velocity;
|
||||
bool headingleft = false;
|
||||
Quaternion targetrot;
|
||||
Rigidbody rb;
|
||||
|
||||
void Start()
|
||||
{
|
||||
anim = GetComponent<Animator>();
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
anim.updateMode = AnimatorUpdateMode.Fixed;
|
||||
anim.animatePhysics = true;
|
||||
#else
|
||||
anim.updateMode = AnimatorUpdateMode.AnimatePhysics;
|
||||
#endif
|
||||
anim.applyRootMotion = true;
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
rb.constraints &= ~RigidbodyConstraints.FreezeRotationY;
|
||||
targetrot = transform.rotation;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
input.x = Input.GetAxis("Horizontal");
|
||||
speed = Mathf.Abs(input.x);
|
||||
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
|
||||
|
||||
// Jump
|
||||
if ((Input.GetKeyDown(jumpJoystick) || Input.GetKeyDown(jumpKeyboard)))
|
||||
{
|
||||
rb.AddForce(new Vector3(0, jumpVelocity, 0), ForceMode.Impulse);
|
||||
}
|
||||
|
||||
// set sprinting
|
||||
if ((Input.GetKeyDown(sprintJoystick) || Input.GetKeyDown(sprintKeyboard)) && input != Vector2.zero)
|
||||
isSprinting = true;
|
||||
if ((Input.GetKeyUp(sprintJoystick) || Input.GetKeyUp(sprintKeyboard)) || input == Vector2.zero)
|
||||
isSprinting = false;
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
anim.SetFloat("Speed", speed);
|
||||
anim.SetBool("isSprinting", isSprinting);
|
||||
|
||||
// Check if direction changes
|
||||
if ((input.x < 0f && !headingleft) || (input.x > 0f && headingleft))
|
||||
{
|
||||
if (input.x < 0f) targetrot = Quaternion.Euler(0, 270, 0);
|
||||
if (input.x > 0f) targetrot = Quaternion.Euler(0, 90, 0);
|
||||
headingleft = !headingleft;
|
||||
}
|
||||
// Rotate player if direction changes
|
||||
var rot = Quaternion.Lerp(rb.rotation, targetrot, Time.deltaTime * 20f);
|
||||
|
||||
// Rigidbody.MoveRotation breaks interpolation for non-kinematic rigidbodies, so don't use it
|
||||
var angle = Vector3.SignedAngle(
|
||||
rb.rotation * Vector3.forward, rot * Vector3.forward, Vector3.up) * Mathf.Deg2Rad / Time.fixedDeltaTime;
|
||||
rb.angularVelocity = Vector3.up * angle;
|
||||
}
|
||||
|
||||
public bool isGrounded()
|
||||
{
|
||||
if (checkGroundForJump)
|
||||
return Physics.Raycast(transform.position, Vector3.down, groundTolerance);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44f5d1b3342d4e34593e4f948d951750
|
||||
timeCreated: 1507745949
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
// WASD to move, Space to sprint
|
||||
public class CharacterMovementNoCamera : MonoBehaviour
|
||||
{
|
||||
public Transform InvisibleCameraOrigin;
|
||||
|
||||
public float StrafeSpeed = 0.1f;
|
||||
public float TurnSpeed = 3;
|
||||
public float Damping = 0.2f;
|
||||
public float VerticalRotMin = -80;
|
||||
public float VerticalRotMax = 80;
|
||||
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
|
||||
public KeyCode sprintKeyboard = KeyCode.Space;
|
||||
|
||||
private bool isSprinting;
|
||||
private Animator anim;
|
||||
private Rigidbody rb;
|
||||
private float currentStrafeSpeed;
|
||||
private Vector2 currentVelocity;
|
||||
private Vector2 input;
|
||||
private Vector2 rotInput;
|
||||
private float speed;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
anim = GetComponent<Animator>();
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
anim.updateMode = AnimatorUpdateMode.Fixed;
|
||||
anim.animatePhysics = true;
|
||||
#else
|
||||
anim.updateMode = AnimatorUpdateMode.AnimatePhysics;
|
||||
#endif
|
||||
anim.applyRootMotion = true;
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
rb.constraints &= ~RigidbodyConstraints.FreezeRotationY;
|
||||
currentVelocity = Vector2.zero;
|
||||
currentStrafeSpeed = 0;
|
||||
isSprinting = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
|
||||
rotInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
|
||||
isSprinting = (Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && speed > 0;
|
||||
|
||||
if (InvisibleCameraOrigin != null)
|
||||
{
|
||||
var rot = InvisibleCameraOrigin.localRotation.eulerAngles;
|
||||
rot.x -= rotInput.y * TurnSpeed;
|
||||
if (rot.x > 180)
|
||||
rot.x -= 360;
|
||||
rot.x = Mathf.Clamp(rot.x, VerticalRotMin, VerticalRotMax);
|
||||
InvisibleCameraOrigin.localRotation = Quaternion.Euler(rot);
|
||||
}
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
anim.SetFloat("Speed", speed);
|
||||
anim.SetFloat("Direction", speed);
|
||||
anim.SetBool("isSprinting", isSprinting);
|
||||
|
||||
// strafing
|
||||
speed = input.y;
|
||||
speed = Mathf.Clamp(speed, -1f, 1f);
|
||||
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref currentVelocity.y, Damping);
|
||||
|
||||
currentStrafeSpeed = Mathf.SmoothDamp(
|
||||
currentStrafeSpeed, input.x * StrafeSpeed, ref currentVelocity.x, Damping);
|
||||
//rb.MovePosition(rb.position + rb.rotation * Vector3.right * currentStrafeSpeed);
|
||||
rb.AddForce(rb.rotation * Vector3.right * currentStrafeSpeed, ForceMode.VelocityChange);
|
||||
|
||||
var euler = rb.rotation.eulerAngles;
|
||||
euler.y += rotInput.x * TurnSpeed;
|
||||
var rot = Quaternion.Euler(euler);
|
||||
|
||||
// Rigidbody.MoveRotation breaks interpolation for non-kinematic rigidbodies, so don't use it
|
||||
var angle = Vector3.SignedAngle(
|
||||
rb.rotation * Vector3.forward, rot * Vector3.forward, Vector3.up) * Mathf.Deg2Rad / Time.fixedDeltaTime;
|
||||
rb.angularVelocity = Vector3.up * angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5200521536b604f40bb6a80aa5a81e30
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user