0
\$\begingroup\$

I'm applying forces to a Rigidbody rb. The forces come from points along a spline splineComputer. The code says "find spline point that is currently nearest to rb, and apply forces to rb toward that spline point's forward direction".

This works, but what is missing is that the forces are applied at a constant speed, whereas I would like the speed to change based on the angle of the spline path.

Since targetDirection is constantly returning a Vector3 forward direction of the nearest spline point, is there some way to say "based on angle of targetDirection, adjust speed to move faster or slow down? Or another approach to try?

public class SplineScript : MonoBehaviour { SplineSample sample; public SplineComputer splineComputer; Rigidbody rb; Vector3 targetDirection; Quaternion targetRotation; public float speed = 60f; void Start() { sample = new(); rb = GetComponent<Rigidbody>(); } void Update() { splineComputer.Project(transform.position, ref sample); targetDirection = sample.forward; targetRotation = Quaternion.LookRotation(new Vector3(targetDirection.x, 0, targetDirection.z)); } private void FixedUpdate() { rb.velocity = targetDirection.normalized * speed * Time.deltaTime; } } 

Overall purpose: Make rigidbodies who enter a River spline to naturally move with its current, even as river flows down slopes

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm not sure I quite understand what you mean when you say "the angle of targetDirection". Is it the angle between targetDirection and the current velocity? Or the rate at which targetDirection changes (i.e. the curvature of the spline)? \$\endgroup\$ Commented Dec 31, 2022 at 9:24

1 Answer 1

1
\$\begingroup\$

Your goal is to make the movement of the rigid body in the river more realistic, in that the speed will change according to the degree of curvature of the current position. This requires modeling the motion of objects in the river.

In the cross section of a river, the velocity of the water flow is not uniform, the velocity of the water in the center of the river is faster than that in the edge:

enter image description here

Of course, the shape of the river will change according to the environment, and the water flow will erode the river bank under the influence of gravity and geostrophic deflection, thus changing the environment. In conclusion, the cross-sectional shape of some river sections is irregular, which affects the flow velocity.

enter image description here

Generally, the water in the river will erode the outer river bank due to inertia, causing the outer river channel to become deeper and the water flow to speed up.

enter image description here

Now let's take a look at why objects moving in the river have constantly changing speeds. Firstly, the speed of the river itself is uneven. Some river sections are relatively narrow, so the water flow speed is higher. When the river turns, the object will continue to move forward due to inertia, thus leaving the area with the highest flow speed and even being washed to the river bank.

enter image description here

For objects floating in the river, they will enter the low-velocity area when the river turns, or hit the river bank to lose kinetic energy, both of which will slow the object down. For human-driven boats, for safety and efficiency reasons, they will apply a force to try to travel in areas of high river flow and away from the river bank.

Based on the above conclusions, I have some suggestions for coding:

  1. Apply a force to a rigidbody instead of setting a velocity and limit the maximum velocity of it.
  2. Or you can set the speed of the river, and get the difference between the object's speed and the river's speed, which is the direction of thrust or drag. The force is proportional to the square of the velocity.
  3. The erosive effect of the water flow is ignored for convenience, i.e. all cross-sections are uniform. Get the distance from the object to the sample, and the velocity of the water flow will attenuate as the distance increases.
  4. If the distance exceeds a set value (half-width of the river), apply a extra force pointing to the sample to simulate the impact of the river bank.
  5. If the object is a boat, always apply a extra force pointing to the sample to simulate the driver's actions.
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.