問題描述
建造一堵提高速度統一的牆 (Make a wall that boost speed unity)
所以...我知道這一定是一種非常直接的事情,但我堅持了好幾天,基本上我需要的是,當一個球撞到牆壁(對撞機)時,它開始加速朝著一個方向,如下圖所示,但我需要使用物理學來完成,我不能只插入位置。
參考解法
方法 1:
You would first need a vector parallel to the ground!
You can use Collider.ClosestPoint
in order to find the closest point on the walls collider to the ball Position.
From this you then know a plane normal for your ground/wall so you can then use Vector3.ProjectOnPlane
in order to convert the usual move direction into one parallel to the ground.
private void FixedUpdate ()
{
var ballRb = ball.GetComponent<Rigidbody>();
var wallCollider = Wall.GetComponent<Collider>();
var hitPoint = wallCollider.ClosestPoint(ballRb.position);
// normal of ground (= vector from hitPoint to ball)
var groundNormal = (ballRb.position ‑ hitPoint).normalized;
// project the given velocity onto the ground
var newVelocity = Vector3.ProjectOnPlane(ballRb.velocity, groundNormal);
// optionally increase the speed of needed e.g.
//var newDirection = newVelocity.normalized;
//var newMagnitude = newVelocity.magnitude * 1.1f; // or any multiplication or addition factor
//newVelocity = newDirection * newMagnitude;
// and finally reassign the new velocity
ballRb.velocity = newVelocity;
}
Note: Typed on smartphone but I hope the idea gets clear and this provides a good start point
(by Renan Klehm、derHugo)