case4
This commit is contained in:
@@ -30,7 +30,7 @@ namespace StudyCase3
|
||||
public float maxDistance = 5f;
|
||||
public LayerMask layerMask;
|
||||
public float itemsRotateSpeed = 120f;
|
||||
public Transform itemRoot;
|
||||
[HideInInspector] public Transform itemRoot;
|
||||
Item selectItem;
|
||||
LineRenderer lineRenderer;
|
||||
private void Awake()
|
||||
|
||||
8
Assets/Scripts/StudyCase4.meta
Normal file
8
Assets/Scripts/StudyCase4.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2a449f5a8745bd4cab6c273662ca0b3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
58
Assets/Scripts/StudyCase4/Item.cs
Normal file
58
Assets/Scripts/StudyCase4/Item.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace StudyCase4
|
||||
{
|
||||
public class Item : MonoBehaviour
|
||||
{
|
||||
Outline outline;
|
||||
bool pick;
|
||||
ThirdCharacterController player;
|
||||
private void Awake()
|
||||
{
|
||||
player = FindAnyObjectByType<ThirdCharacterController>();
|
||||
if (outline == null)
|
||||
outline = gameObject.AddComponent<Outline>();
|
||||
else
|
||||
outline = GetComponent<Outline>();
|
||||
outline.enabled = false;
|
||||
outline.OutlineMode = Outline.Mode.OutlineVisible;
|
||||
outline.OutlineColor = new Color(0, 1, 1);
|
||||
outline.OutlineWidth = 10f;
|
||||
gameObject.layer = LayerMask.NameToLayer("Item");
|
||||
}
|
||||
public void Select()
|
||||
{
|
||||
if(!pick)
|
||||
outline.enabled = true;
|
||||
}
|
||||
public void UnSelect()
|
||||
{
|
||||
if (!pick)
|
||||
outline.enabled = false;
|
||||
}
|
||||
public void PickUp(Transform root)
|
||||
{
|
||||
if (pick) return;
|
||||
pick = true;
|
||||
transform.SetParent(root);
|
||||
outline.OutlineWidth = 2f;
|
||||
gameObject.layer = LayerMask.NameToLayer("Default");
|
||||
player.SetItems();
|
||||
}
|
||||
private void OnMouseEnter()
|
||||
{
|
||||
if (player.pickMode == PickMode.Mouse)
|
||||
Select();
|
||||
}
|
||||
private void OnMouseExit()
|
||||
{
|
||||
if (player.pickMode == PickMode.Mouse)
|
||||
UnSelect();
|
||||
}
|
||||
private void OnMouseDown()
|
||||
{
|
||||
if (player.pickMode == PickMode.Mouse)
|
||||
PickUp(player.itemRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase4/Item.cs.meta
Normal file
11
Assets/Scripts/StudyCase4/Item.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 929a796f4521f344f8b4cd225f1e771a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
177
Assets/Scripts/StudyCase4/ThirdCharacterController.cs
Normal file
177
Assets/Scripts/StudyCase4/ThirdCharacterController.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
|
||||
namespace StudyCase4
|
||||
{
|
||||
public enum PickMode
|
||||
{
|
||||
RayCast,
|
||||
Mouse
|
||||
}
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
[Header("MoveSettings")]
|
||||
CharacterController characterController;
|
||||
InputActionAsset inputAction;
|
||||
Animator animator;
|
||||
Transform forward;
|
||||
Transform model;
|
||||
CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
[Header("PickSettings")]
|
||||
public PickMode pickMode = PickMode.RayCast;
|
||||
public CursorLockMode cursorLock;
|
||||
public bool useDrawRay = true;
|
||||
public float maxDistance = 5f;
|
||||
public LayerMask layerMask;
|
||||
public float itemsRotateSpeed = 120f;
|
||||
public float minCameraDistance = 2f;
|
||||
public float maxCameraDistance = 10f;
|
||||
public float cameraZoomSpeed = 0.1f;
|
||||
[HideInInspector] public Transform itemRoot;
|
||||
Item selectItem;
|
||||
LineRenderer lineRenderer;
|
||||
CinemachineFramingTransposer framingTransposer;
|
||||
private void Awake()
|
||||
{
|
||||
Cursor.lockState = cursorLock;
|
||||
characterController = GetComponent<CharacterController>();
|
||||
forward = transform.Find("Forward");
|
||||
model = transform.Find("Model");
|
||||
animator = model.GetComponentInChildren<Animator>();
|
||||
vCam = transform.Find("Virtual Camera").GetComponent<CinemachineVirtualCamera>();
|
||||
framingTransposer = vCam.GetCinemachineComponent<CinemachineFramingTransposer>();
|
||||
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
|
||||
inputAction.FindAction("Move").started += OnMove;
|
||||
inputAction.FindAction("Move").performed += OnMove;
|
||||
inputAction.FindAction("Move").canceled += OnMove;
|
||||
inputAction.FindAction("Jump").performed += OnJump;
|
||||
inputAction.FindAction("Pick").performed += OnPickUp;
|
||||
inputAction.FindAction("Zoom").started += OnZoom;
|
||||
inputAction.FindAction("Zoom").performed += OnZoom;
|
||||
inputAction.FindAction("Zoom").canceled += OnZoom;
|
||||
inputAction.Enable();
|
||||
itemRoot = transform.Find("ItemRoot");
|
||||
if (lineRenderer == null)
|
||||
{
|
||||
lineRenderer = gameObject.AddComponent<LineRenderer>();
|
||||
//lineRenderer.material = new Material(Shader.Find("Universal Render Pipeline/2D/Sprite-Unlit-Default"));
|
||||
lineRenderer.useWorldSpace = true;
|
||||
}
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
Move();
|
||||
CheckRayHit();
|
||||
ItemsRotate();
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
Debug.Log($"OnMove{context}");
|
||||
if (characterController.isGrounded)
|
||||
moveInput = context.ReadValue<Vector2>();
|
||||
else moveInput = Vector2.zero;
|
||||
}
|
||||
public void OnJump(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.performed && characterController.isGrounded)
|
||||
{
|
||||
moveDir.y = jumpSpeed;
|
||||
}
|
||||
}
|
||||
public void OnZoom(InputAction.CallbackContext context)
|
||||
{
|
||||
float scrollValue = context.ReadValue<float>();
|
||||
float newDistance = framingTransposer.m_CameraDistance + (scrollValue * cameraZoomSpeed * 0.01f);
|
||||
framingTransposer.m_CameraDistance = Mathf.Clamp(newDistance, minCameraDistance, maxCameraDistance);
|
||||
}
|
||||
void Move()
|
||||
{
|
||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||
forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
Debug.Log($"Move");
|
||||
}
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
#region PickUp
|
||||
public void OnPickUp(InputAction.CallbackContext context)
|
||||
{
|
||||
if (pickMode != PickMode.RayCast) return;
|
||||
if (selectItem != null)
|
||||
selectItem.PickUp(itemRoot);
|
||||
selectItem = null;
|
||||
}
|
||||
void CheckRayHit()
|
||||
{
|
||||
lineRenderer.enabled = useDrawRay;
|
||||
if (pickMode != PickMode.RayCast)
|
||||
{
|
||||
useDrawRay = false;
|
||||
return;
|
||||
}
|
||||
Ray ray = new Ray(model.position, model.forward);
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
|
||||
{
|
||||
DrawRay(model.position, hit.point, new Color(0, 1, 1));
|
||||
Item curItem = hit.transform.GetComponent<Item>();
|
||||
if (curItem != null && curItem != selectItem)
|
||||
{
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = curItem;
|
||||
selectItem.Select();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawRay(model.position, model.position+ model.forward* maxDistance, new Color(1, 1, 1));
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = null;
|
||||
}
|
||||
}
|
||||
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
|
||||
{
|
||||
lineRenderer.startColor = lineRenderer.endColor = color;
|
||||
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
|
||||
lineRenderer.positionCount = 2;
|
||||
lineRenderer.SetPosition(0, startPos);
|
||||
lineRenderer.SetPosition(1, endPos);
|
||||
lineRenderer.enabled = true;
|
||||
}
|
||||
void ItemsRotate()
|
||||
{
|
||||
itemRoot.Rotate(0,itemsRotateSpeed*Time.deltaTime, 0);
|
||||
}
|
||||
public void SetItems()
|
||||
{
|
||||
float radius = 1.5f;
|
||||
float angleStep = 360 / itemRoot.childCount;
|
||||
for (int i = 0; i < itemRoot.childCount; i++)
|
||||
{
|
||||
Transform item = itemRoot.GetChild(i);
|
||||
float angle = angleStep * i;
|
||||
Quaternion rot = Quaternion.Euler(0, angle, 0);
|
||||
item.localPosition = rot * itemRoot.forward * radius;
|
||||
item.localScale = Vector3.one * 0.1f;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase4/ThirdCharacterController.cs.meta
Normal file
11
Assets/Scripts/StudyCase4/ThirdCharacterController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59440b4c846906348b9b5ca599c66204
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user