碰撞检测,射线检测,动态位移

This commit is contained in:
lishuo committed 2022-03-11 18:11:56 +08:00
1 parent 16e416332d
commit b3b882c51e
6 files changed
+228 -54

No files matched your search

@@ -22,7 +22,7 @@ namespace Assets.Scripts.Scenes.VideoRide
protected double currentSlope;
protected double nextSlope;
protected double nextSlopeDistance;
protected double distance;
public double distance;
protected double currentSlopeDistance;
protected double lastEndDistance;
protected double totalClimb;
@@ -33,7 +33,8 @@ namespace Assets.Scripts.Scenes.VideoRide
float timer = 1f;
protected bool start = true;
protected VideoGameManager manager { get; set; }
Camera camera;
float currenPlayerHeight;
protected virtual void Start()
{
animator = GetComponent<Animator>();
@@ -41,9 +42,19 @@ namespace Assets.Scripts.Scenes.VideoRide
mapData = manager.GetMapData();
ComputeNextSlope();//初始化坡度等
animator.Play("idle");
camera = Camera.main;
//注解1
//得到模型原始高度
float size_y = GetComponent<Collider>().bounds.size.y;
//得到模型缩放比例
float scal_y = transform.localScale.y;
//它们的乘积就是高度
currenPlayerHeight = (size_y * scal_y);
}
void Update()
protected virtual void Update()
{
timer -= Time.deltaTime;
ComputeAnimator();//控制动画
@@ -58,6 +69,7 @@ namespace Assets.Scripts.Scenes.VideoRide
ComputePlayer();//计算人物属性
ComputeRecord();
ComputeVideo();
ComputeRandom();
}
timer += 1f;
}
@@ -67,8 +79,33 @@ namespace Assets.Scripts.Scenes.VideoRide
speed = 0;
Debug.Log(e.Message);
}
//获取player的position信息
Vector3 pos = transform.position;
pos.y += 3f;
//射线碰撞物信息接收
RaycastHit hit;
//第二种写法
//我们可以先创建出一条射线
Ray ray = new Ray(pos, new Vector3(0, -5, 22));
//然后用这条射线去做射线检测
//射线,输出碰撞体信息,射线长度,射线在那一层检测,射线不忽略开启触发器(istrigger)的碰撞体
bool isHit = Physics.Raycast(ray, out hit, 15f, 1 << 0, QueryTriggerInteraction.Collide);
//在unity窗口画出射线
Debug.DrawLine(pos, new Vector3(pos.x, pos.y, pos.z + 5), Color.red);
if (isHit)//如果射线检测到物体
{
//打印出被检测物体的名字
print(hit.transform.name);
transform.DOLocalMoveX(-2, 1);
transform.DOLocalMoveZ(27, 1).onComplete+=()=>{
transform.DOLocalMoveX(2, 1);
};
}
}
}
//人物碰撞
void OnCollisionEnter(Collision collision)
{
@@ -82,6 +119,17 @@ namespace Assets.Scripts.Scenes.VideoRide
}
}
}
void ComputeRandom()
{
var offset = 30;
if (ticks % 9 == 0)
transform.DOMoveZ(transform.position.z - offset, 1f);
if (ticks % 5 == 0)
transform.DOMoveZ(transform.position.z + offset, 2f);
//人物的rotation y
transform.DOLocalRotate(new Vector3(0f, bearing, 0f), 1f);
}
//动画状态机
void ComputeAnimator()
{
@@ -90,13 +138,26 @@ namespace Assets.Scripts.Scenes.VideoRide
animator.SetFloat("preSpeed", (float)preSpeed);
animator.SetFloat("speed", (float)speed);
animator.SetFloat("grade", (float)currentSlope);
var currentFrame = manager.GetCurrentFrame() ?? 0;
var currentFrame = manager.GetCurrentFrame();
if (manager.mockDirection.ContainsKey(currentFrame))
{
bearing = manager.mockDirection[currentFrame];
}
animator.SetFloat("bearing", bearing);
}
}
//人相对视频不距离不变。 其他人相对视频做前后位移
//人与人之前前后距离按照射线检测 扇形射线
//人与人左右距离按照碰撞检测
//每个人都实时计算前后距离
//其他玩家相对当前主人公做正向或者反向位移,左右
protected virtual void ChangeToFirst()
{
transform.DOLocalMoveZ(-1, 1);
}
protected virtual void ChangeToThird()
{
transform.DOLocalMoveZ(55, 1);
}
//计算人物当前属性
protected abstract void ComputePlayer();
@@ -13,6 +13,8 @@ using System.Linq;
using System.Threading.Tasks;
using TurfCS;
using UnityEngine;
using DG.Tweening;
namespace Assets.Scripts.Scenes.VideoRide
{
public class VideoGameManager : DeviceServiceMonoBase
@@ -34,6 +36,7 @@ namespace Assets.Scripts.Scenes.VideoRide
private double endDistance { get; set; }
public Dictionary<int, bool> slots = new Dictionary<int, bool>();
Transform cube;
private async void Awake()
{
base.Awake();
@@ -49,9 +52,11 @@ namespace Assets.Scripts.Scenes.VideoRide
MapUDPService.Init();//初始化TCP
MockDirection();
InitSlots();
}
void Start()
{
cube = transform.Find("Cube");
//获取媒体播放器
mediaPlayer = FindObjectOfType<MediaPlayer>();
var mapApi = ConfigHelper.mapApi;
@@ -88,6 +93,15 @@ namespace Assets.Scripts.Scenes.VideoRide
cyclingController?.Run(null);
var onlineRiders = cyclingController.riders;
timer += 1f;
}
var p = FindObjectOfType<VideoPlayer>();
if (p.distance > 0)
{
preFrame = currentFrame;
currentFrame = GetCurrentFrame();
var dd = (currentFrame - preFrame) / 200f * cube.position.z;
cube.DOMoveZ((float)(cube.position.z - dd), 0f);
}
}
private void InitSlots()
@@ -195,10 +209,12 @@ namespace Assets.Scripts.Scenes.VideoRide
{
mediaPlayer?.Stop();
}
int preFrame = 0;
int currentFrame = 0;
public int? GetCurrentFrame()
public int GetCurrentFrame()
{
return mediaPlayer?.Control.GetCurrentTimeFrames();
return mediaPlayer.Control.GetCurrentTimeFrames();
}
public void SetCurrentFrame(int seq)
@@ -286,6 +302,10 @@ namespace Assets.Scripts.Scenes.VideoRide
{
return mapData;
}
public float GetPlayRatio()
{
return mediaPlayer.Control.GetPlaybackRate();
}
public List<VideoMapModel> getVideoPointList()
{
return videoPointList;
@@ -12,6 +12,11 @@ namespace Assets.Scripts.Scenes.VideoRide
base.Start();
animator.Play("idle");
}
protected override void Update()
{
base.Update();
}
public void SetPower(double power)
{
this.power = power;
@@ -47,6 +52,7 @@ namespace Assets.Scripts.Scenes.VideoRide
speed = 0;
distance = 0;
}
}
//控制视频播放速度
protected override void ComputeVideo()