问:我想分两部分制作桌球游戏。
1)单机
2)Muliplayer(使用光子在线)
在这里,我试图管理台球桌,8个球和棒子,但我不知道如何将点射线从棒子传递到球子,从而给用户带来击球的想法。或基于触摸的移动棒。
您能给我一些从零开始的方法吗,我可以做到。
答:很高兴听到您的声音。我检查了您要问的问题。下面的代码将为您提供帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayHit : MonoBehaviour
{
public Color c1 = Color.yellow;
public Color c2 = Color.red;
public int lengthOfLineRenderer = 4;
ArrayList<Vector3> linePoints = new ArrayList<Vector3>();
void Start()
{
LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.widthMultiplier = 0.01f;
// lineRenderer.numPositions
lineRenderer.numPositions = lengthOfLineRenderer;
// A simple 2 color gradient with a fixed alpha of 1.0f.
float alpha = 1.0f;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
);
lineRenderer.colorGradient = gradient;
}
void Update()
{
Ray a = new Ray(transform.position, transform.forward);
Ray b;
RaycastHit hit;
if (Deflect(a, out b, out hit))
{
LineRenderer lineRenderer = GetComponent<LineRenderer>();
lineRenderer.SetPosition(0, a.origin);
lineRenderer.SetPosition(1, hit.point);
lineRenderer.SetPosition(2, (b.origin));
lineRenderer.SetPosition(3, b.origin + 3 * b.direction);
}
}
bool Deflect(Ray ray, out Ray deflected, out RaycastHit hit)
{
if (Physics.Raycast(ray, out hit))
{
Vector3 normal = hit.normal;
Vector3 deflect = Vector3.Reflect(ray.direction, normal);
deflected = new Ray(hit.point, deflect);
return true;
}
deflected = new Ray(Vector3.zero, Vector3.zero);
return false;
}
}