您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

44 行
1.2 KiB

using UnityEngine;
/// <summary>
/// Launches projectiles from a touch point with the specified <see cref="initialSpeed"/>.
/// </summary>
[RequireComponent(typeof(Camera))]
public class ProjectileLauncher : MonoBehaviour
{
[SerializeField]
Rigidbody m_ProjectilePrefab;
public Rigidbody projectilePrefab
{
get => m_ProjectilePrefab;
set => m_ProjectilePrefab = value;
}
[SerializeField]
float m_InitialSpeed = 25;
public float initialSpeed
{
get => m_InitialSpeed;
set => m_InitialSpeed = value;
}
void Update()
{
if (m_ProjectilePrefab == null)
return;
if (Input.touchCount == 0)
return;
var touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
var ray = GetComponent<Camera>().ScreenPointToRay(touch.position);
var projectile = Instantiate(m_ProjectilePrefab, ray.origin, Quaternion.identity);
var rigidbody = projectile.GetComponent<Rigidbody>();
rigidbody.velocity = ray.direction * m_InitialSpeed;
}
}
}