请问如下意思的Unity C#代码如何写?
碰撞时的事件CollisionEnter:(注:碰撞物和被碰撞物都未勾选isTrigger)检测被碰撞物的标签,如果标签为A:{生成名为boom的平面预设,位置等于被碰撞...
碰撞时的事件CollisionEnter:
(注:碰撞物和被碰撞物都未勾选isTrigger)
检测被碰撞物的标签,如果标签为A:
{
生成名为boom的平面预设,位置等于被碰撞物当前的位置;
摧毁此被碰撞物;
摧毁此碰撞物;
boom上的VideoPlayer播放视频;
当播放完一次后:
摧毁boom。
}
如果标签为B:
{...}
注:此碰撞物是在运行游戏中被生成的,场景里并没有。因此调用场景里的其他东西好像要加一句话? 展开
(注:碰撞物和被碰撞物都未勾选isTrigger)
检测被碰撞物的标签,如果标签为A:
{
生成名为boom的平面预设,位置等于被碰撞物当前的位置;
摧毁此被碰撞物;
摧毁此碰撞物;
boom上的VideoPlayer播放视频;
当播放完一次后:
摧毁boom。
}
如果标签为B:
{...}
注:此碰撞物是在运行游戏中被生成的,场景里并没有。因此调用场景里的其他东西好像要加一句话? 展开
1个回答
展开全部
逻辑还是挺简单的,唯一注意的是新版本的unity用的VideoClip播放视频
代码就俩
public class MoveObjCtrl : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
string tags = collision.collider.tag;
if (tags == "A")
{
Debug.Log(Time.frameCount + ": " + collision.collider);
Main.Instance.PlayMovie(gameObject, collision.collider.gameObject);
}
else if(tags == "B")
{
}
}
}
这是可移动的碰撞物体上的代码,动态添加
主代码如下
public class Main : MonoBehaviour
{
public static Main Instance = null;
public MoveObjCtrl moveObj = null;
public GameObject boom = null;
public VideoClip videoClip;
void Start()
{
Instance = this;
videoClip = Resources.Load("movie") as VideoClip;
boom = Resources.Load("boom") as GameObject;
GameObject mobj = Instantiate(Resources.Load("moveObj")) as GameObject;
moveObj = mobj.AddComponent<MoveObjCtrl>();
}
void Update()
{
//test
if (moveObj == null)
return;
if(Input.GetKey(KeyCode.W))
{
moveObj.transform.Translate(Vector3.forward*0.1f);
}
if (Input.GetKey(KeyCode.S))
{
moveObj.transform.Translate(Vector3.back * 0.1f);
}
}
public void PlayMovie(GameObject hitObj, GameObject beHitObj)
{
Destroy(hitObj);
Destroy(beHitObj);
GameObject movieobj = Instantiate(boom, beHitObj.transform.position, Quaternion.identity);
var videoPlayer = movieobj.AddComponent<VideoPlayer>();
var audioSource = movieobj.AddComponent<AudioSource>();
videoPlayer.playOnAwake = true;
videoPlayer.clip = videoClip;
videoPlayer.renderMode = VideoRenderMode.MaterialOverride;
videoPlayer.targetMaterialRenderer = movieobj.GetComponent<Renderer>();
videoPlayer.targetMaterialProperty = "_MainTex";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.SetTargetAudioSource(0, audioSource);
Destroy(movieobj, (float)videoClip.length);
}
}
需要工程代码,可以加好友
代码就俩
public class MoveObjCtrl : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
string tags = collision.collider.tag;
if (tags == "A")
{
Debug.Log(Time.frameCount + ": " + collision.collider);
Main.Instance.PlayMovie(gameObject, collision.collider.gameObject);
}
else if(tags == "B")
{
}
}
}
这是可移动的碰撞物体上的代码,动态添加
主代码如下
public class Main : MonoBehaviour
{
public static Main Instance = null;
public MoveObjCtrl moveObj = null;
public GameObject boom = null;
public VideoClip videoClip;
void Start()
{
Instance = this;
videoClip = Resources.Load("movie") as VideoClip;
boom = Resources.Load("boom") as GameObject;
GameObject mobj = Instantiate(Resources.Load("moveObj")) as GameObject;
moveObj = mobj.AddComponent<MoveObjCtrl>();
}
void Update()
{
//test
if (moveObj == null)
return;
if(Input.GetKey(KeyCode.W))
{
moveObj.transform.Translate(Vector3.forward*0.1f);
}
if (Input.GetKey(KeyCode.S))
{
moveObj.transform.Translate(Vector3.back * 0.1f);
}
}
public void PlayMovie(GameObject hitObj, GameObject beHitObj)
{
Destroy(hitObj);
Destroy(beHitObj);
GameObject movieobj = Instantiate(boom, beHitObj.transform.position, Quaternion.identity);
var videoPlayer = movieobj.AddComponent<VideoPlayer>();
var audioSource = movieobj.AddComponent<AudioSource>();
videoPlayer.playOnAwake = true;
videoPlayer.clip = videoClip;
videoPlayer.renderMode = VideoRenderMode.MaterialOverride;
videoPlayer.targetMaterialRenderer = movieobj.GetComponent<Renderer>();
videoPlayer.targetMaterialProperty = "_MainTex";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.SetTargetAudioSource(0, audioSource);
Destroy(movieobj, (float)videoClip.length);
}
}
需要工程代码,可以加好友
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询