case3
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -164,7 +164,6 @@ GameObject:
|
||||
- component: {fileID: 113462435}
|
||||
- component: {fileID: 113462438}
|
||||
- component: {fileID: 113462437}
|
||||
- component: {fileID: 113462436}
|
||||
m_Layer: 0
|
||||
m_Name: Cube
|
||||
m_TagString: Player
|
||||
@@ -187,27 +186,6 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 679676966}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!65 &113462436
|
||||
BoxCollider:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 113462434}
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 0
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 1
|
||||
serializedVersion: 3
|
||||
m_Size: {x: 1, y: 1, z: 1}
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
--- !u!23 &113462437
|
||||
MeshRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -559,7 +537,6 @@ GameObject:
|
||||
- component: {fileID: 679676966}
|
||||
- component: {fileID: 679676969}
|
||||
- component: {fileID: 679676968}
|
||||
- component: {fileID: 679676967}
|
||||
m_Layer: 0
|
||||
m_Name: Capsule
|
||||
m_TagString: Player
|
||||
@@ -583,29 +560,6 @@ Transform:
|
||||
- {fileID: 113462435}
|
||||
m_Father: {fileID: 1390492694}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!136 &679676967
|
||||
CapsuleCollider:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 679676965}
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 0
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 1
|
||||
serializedVersion: 2
|
||||
m_Radius: 0.5
|
||||
m_Height: 2
|
||||
m_Direction: 1
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
--- !u!23 &679676968
|
||||
MeshRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -1161,6 +1115,10 @@ MonoBehaviour:
|
||||
m_CallState: 2
|
||||
m_ActionId: 28dcfdb3-71e3-46bd-9e4a-194bf7ea5554
|
||||
m_ActionName: Controller1/Jump[/Keyboard/space]
|
||||
- m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_ActionId: 00f03ea3-4718-40dc-a561-de734a33a0fa
|
||||
m_ActionName: Controller/Pick[/Keyboard/f]
|
||||
m_NeverAutoSwitchControlSchemes: 0
|
||||
m_DefaultControlScheme:
|
||||
m_DefaultActionMap: Controller
|
||||
@@ -1179,13 +1137,20 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
characterController: {fileID: 2046150434}
|
||||
animator: {fileID: 0}
|
||||
forward: {fileID: 91895208}
|
||||
model: {fileID: 1390492694}
|
||||
hitItems: []
|
||||
vCam: {fileID: 1414446907}
|
||||
moveSpeed: 5
|
||||
jumpSpeed: 2
|
||||
turnSpeed: 10
|
||||
gravity: 10
|
||||
maxDistance: 100
|
||||
layerMask:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
areaPick: 0
|
||||
--- !u!1 &2094812806
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94148b451f3cd28429d6e1540ba2ecf7
|
||||
guid: bc1d0d9e59c1dff4f964b35379f9a1d9
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
|
||||
4077
Assets/Scenes/StudyCase3.unity
Normal file
4077
Assets/Scenes/StudyCase3.unity
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc1d0d9e59c1dff4f964b35379f9a1d9
|
||||
guid: 94148b451f3cd28429d6e1540ba2ecf7
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
8
Assets/Scripts/StudyCase1.meta
Normal file
8
Assets/Scripts/StudyCase1.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef62159c54197e549a1875fdf43a8aa7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
46
Assets/Scripts/StudyCase1/ThirdCharacterController.cs
Normal file
46
Assets/Scripts/StudyCase1/ThirdCharacterController.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace StudyCase1
|
||||
{
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public Cinemachine.CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
private void Update()
|
||||
{
|
||||
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)
|
||||
{
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Assets/Scripts/StudyCase2.meta
Normal file
8
Assets/Scripts/StudyCase2.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b5277eb47b485942af6e02d82cbe449
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
50
Assets/Scripts/StudyCase2/ThirdCharacterController.cs
Normal file
50
Assets/Scripts/StudyCase2/ThirdCharacterController.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace StudyCase2
|
||||
{
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public Cinemachine.CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
private void Update()
|
||||
{
|
||||
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);
|
||||
}
|
||||
else animator?.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase2/ThirdCharacterController.cs.meta
Normal file
11
Assets/Scripts/StudyCase2/ThirdCharacterController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd1d558f25085d447a5e81522e221905
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/StudyCase3.meta
Normal file
8
Assets/Scripts/StudyCase3.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35960d40ccba217429ab4b8d7c83c5e5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
Assets/Scripts/StudyCase3/Item.cs
Normal file
45
Assets/Scripts/StudyCase3/Item.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace StudyCase3
|
||||
{
|
||||
public class Item : MonoBehaviour
|
||||
{
|
||||
public bool pick;
|
||||
Outline outline;
|
||||
private void Awake()
|
||||
{
|
||||
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.OutlineWidth = 10f;
|
||||
gameObject.layer = LayerMask.NameToLayer("Item");
|
||||
}
|
||||
public void Select()
|
||||
{
|
||||
if(!pick)
|
||||
outline.enabled = true;
|
||||
}
|
||||
public void UnSelect()
|
||||
{
|
||||
if (!pick)
|
||||
outline.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase3/Item.cs.meta
Normal file
11
Assets/Scripts/StudyCase3/Item.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77f9323b319c41c488194a8072690034
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
120
Assets/Scripts/StudyCase3/ThirdCharacterController.cs
Normal file
120
Assets/Scripts/StudyCase3/ThirdCharacterController.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace StudyCase3
|
||||
{
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public Transform items;
|
||||
public 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;
|
||||
Item selectItem;
|
||||
private void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
Move();
|
||||
RotationItems();
|
||||
CheckRayHit();
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext 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 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);
|
||||
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);
|
||||
}
|
||||
else animator?.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
void CheckRayHit()
|
||||
{
|
||||
Ray ray = new Ray(model.position, model.forward);
|
||||
RaycastHit hit;
|
||||
if(Physics.Raycast(ray,out hit, maxDistance, layer))
|
||||
{
|
||||
Item curItem = hit.collider.GetComponent<Item>();
|
||||
if (curItem && curItem != selectItem)
|
||||
{
|
||||
if(selectItem!=null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = curItem;
|
||||
selectItem.Select();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = null;
|
||||
}
|
||||
}
|
||||
void RotationItems()
|
||||
{
|
||||
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++)
|
||||
{
|
||||
Transform item = items.GetChild(i);
|
||||
float angle = i * angleStep;
|
||||
Quaternion rotation = Quaternion.Euler(0, angle, 0);
|
||||
item.localPosition = rotation * Vector3.forward * radius;
|
||||
item.localScale = Vector3.one * 0.1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase3/ThirdCharacterController.cs.meta
Normal file
11
Assets/Scripts/StudyCase3/ThirdCharacterController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c97885bac137e514d8f8607b1ac78653
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,73 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public List<GameObject> hitItems = new List<GameObject>();
|
||||
public Cinemachine.CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
public float maxDistance = 100f;
|
||||
public LayerMask layerMask;
|
||||
public bool areaPick;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
private void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
Move();
|
||||
}
|
||||
public void CheckRayHit()
|
||||
{
|
||||
Ray ray = new Ray(vCam.transform.position, vCam.transform.forward);
|
||||
RaycastHit raycastHit;
|
||||
if(Physics.Raycast(ray,out raycastHit, maxDistance, layerMask))
|
||||
{
|
||||
Debug.Log($"CheckRayHit:{raycastHit.collider.gameObject.name}");
|
||||
hitItems[0] = raycastHit.collider.gameObject;
|
||||
}
|
||||
}
|
||||
public 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);
|
||||
}
|
||||
else animator?.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext 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 OnPickUp(InputAction.CallbackContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ TagManager:
|
||||
-
|
||||
- Water
|
||||
- UI
|
||||
-
|
||||
-
|
||||
- Item
|
||||
- Npc
|
||||
-
|
||||
-
|
||||
-
|
||||
|
||||
Reference in New Issue
Block a user