Commit f38e0c14 authored by 杨泽宇's avatar 杨泽宇

更新射箭游戏

parent 10bbe63d
......@@ -12,5 +12,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 690929a59dc7a42da9030305190d391f, type: 3}
m_Name: XRDeviceSimulatorSettings
m_EditorClassIdentifier:
m_AutomaticallyInstantiateSimulatorPrefab: 1
m_SimulatorPrefab: {fileID: 6598815579406187037, guid: 18ddb545287c546e19cc77dc9fbb2189, type: 3}
m_AutomaticallyInstantiateSimulatorPrefab: 0
m_SimulatorPrefab: {fileID: 0}
This diff is collapsed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class ArrowController : MonoBehaviour
{
// Start is called before the first frame update
public GameObject arrowPrefab;
public GameObject arrowPlace;
public GameObject arrowTrigger;
public GameObject arrowBack;
public Vector3 arrowHeading;
public bool waitingToShoot = false;
void Start()
{
arrowPlace = GameObject.Find("XR Interaction Setup/XR Origin (XR Rig)/Camera Offset/Main Camera/ArrowPlace");
arrowTrigger = GameObject.Find("Bow/ArrowTrigger");
arrowBack = GameObject.Find("Bow/String/Notch");
}
// Update is called once per frame
void Update()
{
arrowHeading = arrowTrigger.transform.position - arrowBack.transform.position;
if (waitingToShoot)
{
gameObject.transform.position = arrowBack.transform.position + gameObject.transform.localScale.y / 2.4f * arrowHeading.normalized;
gameObject.transform.rotation = Quaternion.LookRotation(arrowHeading, Vector3.forward);
}
}
public void GrabArrow()
{
GameObject arrow = Instantiate(arrowPrefab, arrowPlace.transform.position, arrowPlace.transform.rotation);
arrow.transform.parent = GameObject.Find("XR Interaction Setup/XR Origin (XR Rig)/Camera Offset/Main Camera").transform;
}
public void ReleaseArrow()
{
gameObject.GetComponent<Rigidbody>().useGravity = true;
gameObject.GetComponent<Rigidbody>().isKinematic = false;
gameObject.transform.parent = null;
if (arrowTrigger.GetComponent<ArrowTrigger>().arrowOnBow == gameObject)
{
gameObject.GetComponent<Rigidbody>().useGravity = false;
gameObject.transform.parent = arrowTrigger.transform;
waitingToShoot = true;
}
Debug.Log("ReleaseArrow:" + gameObject.transform.parent);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Target")
{
gameObject.transform.parent = other.transform;
gameObject.GetComponentInParent<Rigidbody>().velocity = new Vector3(0, 0, 0);
gameObject.GetComponentInParent<Rigidbody>().useGravity = false;
gameObject.GetComponentInParent<Rigidbody>().isKinematic = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Target")
{
gameObject.transform.parent = null;
gameObject.GetComponentInParent<Rigidbody>().useGravity = true;
gameObject.GetComponentInParent<Rigidbody>().constraints = RigidbodyConstraints.None;
gameObject.GetComponentInParent<Rigidbody>().isKinematic = false;
}
}
}
fileFormatVersion: 2
guid: a5c80d12c595e834eaf04fc07ee37b38
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowTrigger : MonoBehaviour
{
public GameObject arrowOnBow;
private bool arrowStatus;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Arrow")
{
arrowStatus = true;
arrowOnBow = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Arrow")
{
arrowStatus = false;
arrowOnBow = null;
}
}
}
fileFormatVersion: 2
guid: d0db9fd0bb60ef34faae5098fe6fae6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 弓控制器
/// </summary>
public class BowController : MonoBehaviour
{
public float arrowSpeed = 400;
public GameObject arrowTrigger;
public GameObject arrowAttachedPoint;
public GameObject stringStartPoint;
public GameObject stringEndPoint;
public GameObject stringMiddlePoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ReleaseString()
{
Debug.Log("ReleaseString");
stringMiddlePoint.transform.position = (stringStartPoint.transform.position + stringEndPoint.transform.position + new Vector3(0, 0.07f, 0)) / 2;
stringMiddlePoint.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
stringMiddlePoint.GetComponent<Rigidbody>().angularVelocity = new Vector3(0, 0, 0);
if (arrowTrigger.GetComponent<ArrowTrigger>().arrowOnBow)
{
GameObject arrowOnBow = arrowTrigger.GetComponent<ArrowTrigger>().arrowOnBow;
arrowOnBow.transform.parent = null;
arrowOnBow.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
arrowOnBow.GetComponent<Rigidbody>().useGravity = true;
arrowOnBow.GetComponent<ArrowController>().waitingToShoot = false;
arrowOnBow.GetComponent<Rigidbody>().AddForce(arrowOnBow.GetComponent<ArrowController>().arrowHeading *
arrowOnBow.GetComponent<ArrowController>().arrowHeading.magnitude * arrowSpeed);
}
}
}
fileFormatVersion: 2
guid: a9bd1017b01dad845a964a4becf11c9a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class PullMeasurer : XRBaseInteractable
{
[SerializeField] private Transform start; // 拉伸测量的起始点
[SerializeField] private Transform end; // 拉伸测量的结束点
public float PullAmount { get; private set; } = 0.0f; // 当前拉伸的程度,初始值为 0
// 通过线性插值获取当前拉伸位置的方法
public Vector3 PullPosition => Vector3.Lerp(start.position, end.position, PullAmount);
protected override void OnSelectExited(SelectExitEventArgs args)
{
base.OnSelectExited(args);
PullAmount = 0; // 当交互结束时,重置拉伸程度为 0
}
public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
{
base.ProcessInteractable(updatePhase);
if (isSelected)
{
// 在物体被选中时更新拉伸值
if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic)
UpdatePull();
}
}
private void UpdatePull()
{
// 使用交互者的位置来计算拉伸程度
Vector3 interactorPosition = firstInteractorSelecting.transform.position;
// 计算新的拉伸程度,并返回它在空间中的位置
PullAmount = CalculatePull(interactorPosition);
}
private float CalculatePull(Vector3 pullPosition)
{
// 计算方向和长度
Vector3 pullDirection = pullPosition - start.position;
Vector3 targetDirection = end.position - start.position;
// 规范化目标方向
float maxLength = targetDirection.magnitude;
targetDirection.Normalize();
// 计算拉伸值
float pullValue = Vector3.Dot(pullDirection, targetDirection) / maxLength;
return Mathf.Clamp(pullValue, 0.0f, 1.0f); // 将拉伸值限制在 0 到 1 之间
}
private void OnDrawGizmos()
{
// 在场景中绘制起始点到结束点的连线
if (start && end)
Gizmos.DrawLine(start.position, end.position);
}
}
fileFormatVersion: 2
guid: 25fb566416268a1409511aab6f4e2d73
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 弓弦的绘制
/// </summary>
public class StringRenderer : MonoBehaviour
{
[Header("渲染位置")]
[SerializeField] private Transform start; // 弓弦的起始点
[SerializeField] private Transform middle; // 弓弦的中间点
[SerializeField] private Transform end; // 弓弦的结束点
private LineRenderer lineRenderer; // 用于渲染弓弦的 LineRenderer 组件
private void Awake()
{
lineRenderer = GetComponent<LineRenderer>(); // 获取当前游戏对象上的 LineRenderer 组件
}
private void Update()
{
if (Application.isEditor && !Application.isPlaying)
UpdatePositions(); // 在编辑器模式下,每帧更新弓弦的位置
}
private void OnEnable()
{
Application.onBeforeRender += UpdatePositions; // 在渲染之前更新弓弦的位置
}
private void OnDisable()
{
Application.onBeforeRender -= UpdatePositions; // 移除在渲染之前更新弓弦的位置的监听
}
/// <summary>
/// 更新弓弦的位置
/// </summary>
private void UpdatePositions()
{
// 设置 LineRenderer 组件的顶点位置为起始点、中间点和结束点的位置
lineRenderer.SetPositions(new Vector3[] { start.position, middle.position, end.position });
}
}
fileFormatVersion: 2
guid: cd5065d3a03425c40a7a8a0c97d287d7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment