rigidbody with no drag, isKinematic and Continue Collision Detection
mesh collider with ice
If I apply Continuous collision detection to the ship, when it hits the walls of the tunnel the ship stops moving forward. If I leave it on Discrete detection, the ship slides, but sometimes penetrates the wall.
I am moving the ship forward using rigidbody.velocity.
Any thoughts on better collision detection without causing the ship to stop?
(desiredVelocity - rigidbody.velocity) will maintain the force required to keep your ship moving at the desired velocity. However, that will make it accelerate almost instantly, so you have to Clamp it to a maximum of acceleration.
For a ship, I’d say this is pretty much all you’d need to do.
@devikkw: I didn’t import with colliders, but that didn’t help much but good idea. I had forgotten that.
@Tuah: I’m having trouble adapting your script to mine. In my adaptation, the ship is pushed more then directed along the z axis. I need the ship to steer like a car on the road… when I tell it to turn, I want it to turn. I don’t want it to feel like a boat in the water.
My previous script:
thrust = 300 * Time.deltaTime;
rigidbody.velocity = transform.localRotation * new Vector3*(0, 0, thrust);
Attempt 1:
rigidbody.AddForce( (transform.localRotation * new Vector3(0, 0, thrust))-rigidbody.velocity );
Object in motion stays in motion. Untill it is resisted by another force (drag).
But since the ship doesnt have wheels to apply drag when sliding sideways you need to,
Add more Drag.
Or,
Get the local verocity of the ships X axis and apply force in the opposing direction of the X verocity.
You’re adding a few things that aren’t necessary, but it’s an easy fix. Sorry if my script is messy at all.
First off, all you need is the direction you want to move. Set this how you want, in script. And speed and acceleration.
float topSpeed = 300; // Meters per Second. (Probably a bit too fast, in my opinion. Your game though.)
float acceleration = 30; // Or whatever
Vector3 desiredDirection; // (Maximum magnitude of 1.)
void FixedUpdate() {
rigidbody.AddForce( Mathf.Clamp(desiredDirection*topSpeed, 0, acceleration), ForceMode.Acceleration );
}
Now, this will make it so that it works more realistically, so doing a complete turn will take a little extra time, since the ship has to apply more force to move in that direction. If you want it tighter, increase Acceleration, or try using ForceMode.VelocityChange.
Your desiredDirection can be anything you choose. I suppose since you want it to be the ship’s “forward” direction, you could make desiredDirection transform.forward or something.
If it has a magnitude of 1, the AddForce will try to reach the full TopSpeed. So if the magnitude of desiredDirection is lower, the ship will try to reach a lower velocity, allowing the player to slow to a stop if you desire.