case3
This commit is contained in:
@@ -4,30 +4,19 @@ namespace StudyCase3
|
||||
{
|
||||
public class Item : MonoBehaviour
|
||||
{
|
||||
public bool pick;
|
||||
Outline outline;
|
||||
bool pick;
|
||||
ThirdCharacterController player;
|
||||
private void Awake()
|
||||
{
|
||||
player = FindAnyObjectByType<ThirdCharacterController>();
|
||||
if (outline == null)
|
||||
outline = gameObject.AddComponent<Outline>();
|
||||
else
|
||||
outline = GetComponent<Outline>();
|
||||
outline.OutlineMode = Outline.Mode.OutlineVisible;
|
||||
outline.OutlineColor = new Color(0,1,1);
|
||||
outline.OutlineWidth = 10f;
|
||||
outline.enabled = false;
|
||||
gameObject.layer = LayerMask.NameToLayer("Item");
|
||||
}
|
||||
public void PickUp(Transform root)
|
||||
{
|
||||
pick = true;
|
||||
outline.OutlineWidth = 2f;
|
||||
gameObject.layer = LayerMask.NameToLayer("Default");
|
||||
transform.SetParent(root);
|
||||
}
|
||||
public void UnPickUp()
|
||||
{
|
||||
pick = false;
|
||||
outline.OutlineMode = Outline.Mode.OutlineVisible;
|
||||
outline.OutlineColor = new Color(0, 1, 1);
|
||||
outline.OutlineWidth = 10f;
|
||||
gameObject.layer = LayerMask.NameToLayer("Item");
|
||||
}
|
||||
@@ -41,5 +30,29 @@ namespace StudyCase3
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77f9323b319c41c488194a8072690034
|
||||
guid: d915cfc36d9949f419c265a71b18203c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
|
||||
@@ -1,39 +1,60 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace StudyCase3
|
||||
{
|
||||
public enum PickMode
|
||||
{
|
||||
RayCast,
|
||||
Mouse
|
||||
}
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public Transform items;
|
||||
public Cinemachine.CinemachineVirtualCamera vCam;
|
||||
|
||||
[Header("MoveSettings")]
|
||||
CharacterController characterController;
|
||||
InputActionAsset inputAction;
|
||||
Animator animator;
|
||||
Transform forward;
|
||||
Transform model;
|
||||
Cinemachine.CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
public bool pickUpByCollider;
|
||||
public float maxDistance = 5f;
|
||||
public LayerMask layer;
|
||||
public float itemsRotateSpeed = 100f;
|
||||
|
||||
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 Transform itemRoot;
|
||||
Item selectItem;
|
||||
private void Start()
|
||||
LineRenderer lineRenderer;
|
||||
private void Awake()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.lockState = cursorLock;
|
||||
characterController = GetComponent<CharacterController>();
|
||||
forward = transform.Find("Forward");
|
||||
model = transform.Find("Model");
|
||||
animator = model.GetComponentInChildren<Animator>();
|
||||
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
|
||||
inputAction = Resources.Load<InputActionAsset>("Player");
|
||||
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.Enable();
|
||||
itemRoot = transform.Find("ItemRoot");
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
Move();
|
||||
RotationItems();
|
||||
CheckRayHit();
|
||||
ItemsRotate();
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
@@ -48,15 +69,6 @@ namespace StudyCase3
|
||||
moveDir.y = jumpSpeed;
|
||||
}
|
||||
}
|
||||
public void OnPickUp(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.performed && selectItem!=null)
|
||||
{
|
||||
selectItem.PickUp(items);
|
||||
selectItem = null;
|
||||
RearrangeItems();
|
||||
}
|
||||
}
|
||||
void Move()
|
||||
{
|
||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||
@@ -73,16 +85,26 @@ namespace StudyCase3
|
||||
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()
|
||||
{
|
||||
if (pickMode != PickMode.RayCast) return;
|
||||
Ray ray = new Ray(model.position, model.forward);
|
||||
RaycastHit hit;
|
||||
if(Physics.Raycast(ray,out hit, maxDistance, layer))
|
||||
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
|
||||
{
|
||||
Item curItem = hit.collider.GetComponent<Item>();
|
||||
if (curItem && curItem != selectItem)
|
||||
DrawRay(model.position, hit.point, new Color(0, 1, 1));
|
||||
Item curItem = hit.transform.GetComponent<Item>();
|
||||
if (curItem != null && curItem != selectItem)
|
||||
{
|
||||
if(selectItem!=null)
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = curItem;
|
||||
selectItem.Select();
|
||||
@@ -90,31 +112,49 @@ namespace StudyCase3
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawRay(model.position, model.position+ model.forward* maxDistance, new Color(1, 1, 1));
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = null;
|
||||
}
|
||||
}
|
||||
void RotationItems()
|
||||
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
|
||||
{
|
||||
items.transform.Rotate(0, itemsRotateSpeed*Time.deltaTime, 0);
|
||||
}
|
||||
void RearrangeItems()
|
||||
{
|
||||
int childCount = items.childCount;
|
||||
if (childCount == 0) return;
|
||||
|
||||
float radius = 1.5f;
|
||||
float angleStep = 360f / childCount;
|
||||
|
||||
for (int i = 0; i < childCount; i++)
|
||||
if (lineRenderer == null)
|
||||
{
|
||||
Transform item = items.GetChild(i);
|
||||
float angle = i * angleStep;
|
||||
Quaternion rotation = Quaternion.Euler(0, angle, 0);
|
||||
item.localPosition = rotation * Vector3.forward * radius;
|
||||
lineRenderer = gameObject.AddComponent<LineRenderer>();
|
||||
lineRenderer.material = new Material(Shader.Find("Universal Render Pipeline/2D/Sprite-Unlit-Default"));
|
||||
lineRenderer.useWorldSpace = true;
|
||||
}
|
||||
if (!useDrawRay)
|
||||
{
|
||||
lineRenderer.enabled = false;
|
||||
return;
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c97885bac137e514d8f8607b1ac78653
|
||||
guid: 460a2b895aff7e848bd63babe00d6f49
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
|
||||
Reference in New Issue
Block a user