case3修改

This commit is contained in:
2025-11-27 16:23:06 +08:00
parent fa4d844d13
commit 44b3a16800
11 changed files with 47 additions and 24 deletions

View File

@@ -115,6 +115,10 @@ namespace StudyCase3
<img src="/image/studycase3/道具挂载脚本.png" data-fancybox="gallery"/>
- 创建Pick输入事件绑定按键F
<img src="/image/studycase3/创建Pick输入事件绑定F.png" data-fancybox="gallery"/>
- 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
@@ -160,7 +164,7 @@ namespace StudyCase3
model = transform.Find("Model");
animator = model.GetComponentInChildren<Animator>();
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
inputAction = Resources.Load<InputActionAsset>("Player");
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
@@ -168,6 +172,12 @@ namespace StudyCase3
inputAction.FindAction("Pick").performed += OnPickUp;
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()
{
@@ -214,7 +224,12 @@ namespace StudyCase3
}
void CheckRayHit()
{
if (pickMode != PickMode.RayCast) return;
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))
@@ -239,17 +254,6 @@ namespace StudyCase3
}
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
{
if (lineRenderer == null)
{
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;