Que*_*n3r 2 c# unity-game-engine
射弹时我执行
private Rigidbody rigid;
private Vector3 currentMovementDirection;
private void FixedUpdate()
{
rigid.velocity = currentMovementDirection;
}
public void InitProjectile(Vector3 startPosition, Quaternion startRotation)
{
transform.position = startPosition;
transform.rotation = startRotation;
currentMovementDirection = transform.forward;
}
Run Code Online (Sandbox Code Playgroud)
我将其InitProjectile
用作我的Start
方法,因为我不会销毁对象,而是回收它并禁用渲染器。
当用射弹击中物体时,该射弹应该被反射。
我拍了一堵墙,这些墙多次反射物体
我认为Unity为此提供了一些东西
https://docs.unity3d.com/ScriptReference/Vector3.Reflect.html
当碰撞被触发时
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == projectile)
{
projectileComponent.ReflectProjectile(); // reflect it
}
}
Run Code Online (Sandbox Code Playgroud)
我想通过使用来反映它
public void ReflectProjectile()
{
// Vector3.Reflect(... , ...);
}
Run Code Online (Sandbox Code Playgroud)
但是我必须使用什么作为参数Reflect
?看来我必须旋转弹丸才能改变其运动方向。
为了反映弹丸操纵刚体的速度。在这个例子中,我使用一个立方体(上面有这个脚本)作为射弹,周围有一些立方体。
没有碰撞器被标记为触发器。这是它的外观:https : //youtu.be/2Lfj-li6x8M
public class testvelocity : MonoBehaviour
{
private Rigidbody _rb;
private Vector3 _velocity;
// Use this for initialization
void Start()
{
_rb = this.GetComponent<Rigidbody>();
_velocity = new Vector3(3f, 4f, 0f);
_rb.AddForce(_velocity, ForceMode.VelocityChange);
}
void OnCollisionEnter(Collision collision){
ReflectProjectile(_rb, collision.contacts[0].normal);
}
private void ReflectProjectile(Rigidbody rb, Vector3 reflectVector)
{
_velocity = Vector3.Reflect(_velocity, reflectVector);
_rb.velocity = _velocity;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6404 次 |
最近记录: |