C# 物体撞击墙壁后从墙上反射出来
上个星期我一直在努力解决这个问题。首先,我目前正在进行一个Unity项目,下面是我一直坚持的代码:C# 物体撞击墙壁后从墙上反射出来,c#,unity3d,rotation,C#,Unity3d,Rotation,上个星期我一直在努力解决这个问题。首先,我目前正在进行一个Unity项目,下面是我一直坚持的代码: private RaycastHit hit; private Ray cast; private bool casthit; void Start() { casthit = false; Ray cast = new Ray(transform.position, transform.forward); } void OnTriggerEnter(Collider o
private RaycastHit hit;
private Ray cast;
private bool casthit;
void Start() {
casthit = false;
Ray cast = new Ray(transform.position, transform.forward);
}
void OnTriggerEnter(Collider other){
if (other.tag == "Enemy") {
other.GetComponent<BasicEnemy>().setHealthLower(damage);
}
if (other.tag != "Turret" && other.tag != "Bullet") {
//here is where we want to reflect this object
}
}
void Update () {
Ray cast = new Ray(transform.position, transform.forward);
Physics.Raycast (cast, out hit);
transform.position += transform.forward * speed * Time.deltaTime;
}
private RaycastHit;
私人射线投射;
私人布尔卡斯特希特;
void Start(){
casthit=false;
光线投射=新光线(transform.position,transform.forward);
}
无效对撞机(对撞机其他){
if(other.tag==“敌人”){
其他.GetComponent().setHealthLower(损坏);
}
if(other.tag!=“炮塔”&&other.tag!=“子弹”){
//这里是我们想要反映这个物体的地方
}
}
无效更新(){
光线投射=新光线(transform.position,transform.forward);
物理.光线投射(投射,外击);
transform.position+=transform.forward*速度*时间.deltaTime;
}
基本上,我有一个发射子弹的炮塔,当其中一颗子弹击中墙壁时,我希望它从墙上反射出来(或者从墙上反弹,如果你喜欢这个术语的话)。墙壁反射的方式应与光线从表面反射的方式相同。现在的问题主要是,我不知道这些东西是如何工作的,特别是因为我基本上需要计算出子弹要朝向的角度(所以我可以给变换一个y值,这样它就会继续向下一个方向移动)
正如你所看到的,我已经有了一些RaycStudio和一个命中射线的基础(因为我相信我们需要在一个点上正常的命中射线)。 不幸的是,我只是不知道从哪里开始计算,甚至不知道我需要做什么类型的计算。通常的
Vector3.reflect
到现在为止还没有工作(或者至少在我以前的任何计算中都没有)现在我遇到了一个路障,我想不出新的东西来尝试,所以我非常希望你们中的一个知道如何设法做到这一点
已经谢谢了!我(终于)找到了自己问题的答案 对于任何感兴趣的人,这里是新代码:
void OnTriggerEnter(Collider other){
if (other.tag == "Enemy") {
other.GetComponent<BasicEnemy>().setHealthLower(damage);
}
if (other.tag != "Turret" && other.tag != "Bullet") {
Vector3 reflectposition = -2 * (Vector3.Dot(transform.position, hit.normal) * hit.normal - transform.position);
reflectposition.y = this.transform.position.y;
this.transform.LookAt(reflectposition);
}
}
void Update () {
Ray cast = new Ray(transform.position, transform.forward);
Physics.Raycast (cast, out hit);
transform.position += transform.forward * speed * Time.deltaTime;
}
void ontriggenter(碰撞器其他){
if(other.tag==“敌人”){
其他.GetComponent().setHealthLower(损坏);
}
if(other.tag!=“炮塔”&&other.tag!=“子弹”){
Vector3 reflectposition=-2*(Vector3.Dot(transform.position,hit.normal)*hit.normal-transform.position);
reflectposition.y=this.transform.position.y;
this.transform.LookAt(反射位置);
}
}
无效更新(){
光线投射=新光线(transform.position,transform.forward);
物理.光线投射(投射,外击);
transform.position+=transform.forward*速度*时间.deltaTime;
}
你可以去老派学校读一本关于基本3d向量数学的书/文章…(这不是一个很好的建议)…Vector3.Reflect
。你给它一个输入向量和它反射的曲面的法线,它给你结果向量。@SirBraneDamuj我试过Vector3.Reflect,但我似乎无法正确获得输入向量。我认为它应该是transform.position,但问题是我没有尝试为了反映位置,事实上我正试图反映旋转,所以向量3.使用变换进行反射。位置不起作用,变换。旋转是一个四元数,所以它不起作用。你为什么不使用内置的物理并重新发明你自己的解呢?@Tom嘿,很抱歉回答得太晚了,但是是的当时有。我不能使用普通的统一物理的原因是因为它们是基于帧的,所以如果子弹物体在某一点在墙的一边,而下一刻他在另一边,因为子弹以很快的速度运动,这意味着统一物理永远不会执行。统一物理对小事情很有用,但是对于这类东西,物理学有点劣质