- Home /
3rd person Flying with Collision problems
I'm trying to make a 3rd person flight simulator-type game that lets you collide with terrain and buildings without bouncing. At first I tried using rigid bodies but that created bounce, and I just want the player to stop or back up or turn or go in any direction except through the object when they collide with something.
The problem is that when I add a Character Controller to my player prefab, it can fly through the terrain and buildings too, even though they have collider meshes (box colliders mostly). From searching, I think that the character controller has to detect collision events, but I don't know how to do this (though I keep trying).
I tried adding a rigidbody but it bounces, and I don't want it to bounce. It's a spaceship... it should just float in another direction.
I currently have a movement.cs script attached to my player with basic movement, up down, rotation, etc. It works great. My player is a character controller.
I want the player to just stop moving when it collides with the terrain or anything with a primitive collider.
I tried this in my movement script, but I don't understand how to GET the Collision in order to pass the parameter to this method.
void OnCollisionEnter(Collision theCollision)
{
if(theCollision.gameObject.name != "Player")
{
Debug.Log("Stop moving");
MoveSpeed = -MoveSpeed * 0.1;
}
}
I hope that makes sense. I've been working on this for a while and it's hard to step back and explain it, and I KNOW it's a simple problem. I've seen dozens of people posting about it, but either their solution wasn't made public or they changed it to a rigidbody or I didn't quite understand something in their solution.
Please, if anyone can give me a hand, I'd really appreciate the help. Also, if there is a different way to accomplish my task, any advice would be wonderful. Thank you.
Answer by syclamoth · May 02, 2012 at 01:14 AM
For starters, don't use a CharacterController for a flight simulator. Just don't. It's asking for far more trouble than it's worth.
Do you know about physic materials? Try adding a physic material to your rigidbody that doesn't bounce. This is a pretty straightforward issue.
Also, if you want the rigidbody to instantly stop, you can use
rigidbody.velocity = Vector3.zero;
or
rigidbody.velocity = (-rigidbody.velocity * 0.2f);
I never heard of Physic material before, so that is awesome. Thank you so much for mentioning it.
I changed my controller to a rigid body, and I believe it is working. But it's hard to tell since my camera is now spinning wildly and is jerky when moved... I'm trying to figure out why this is happening. I read from other posts about the issue that it's because the camera is attached to a rigid body. Do you know how to fix the camera?
Thanks very much again, I really do appreciate the heads-up on the Physic material! =)
$$anonymous$$ake the connection between the camera and the player a bit less direct. Ins$$anonymous$$d of the camera being a direct child of the player, make it have some kind of interpolated 'follow' script- maybe one that doesn't follow rotation.
Thanks Syclamoth - I tried this out by making an independent Camera and giving it a follow script:
public class Follow : $$anonymous$$onoBehaviour
{
public Transform followTarget;
public Transform camera;
public float offsetX, offsetY, offsetZ;
private Vector3 offset;
// Use this for initialization
void Start ()
{
offset = new Vector3(offsetX, offsetY, offsetZ);
}
// Update is called once per frame
void Update ()
{
camera.position = followTarget.position + offset;
}
}
Then I set the Camera to the Camera that owns the script, and the follow was set to the spaceship player object.
It doesn't begin spinning, so that's a step forward! But now it spins whenever I move, and I don't think the offset works because it still moves the camera to the exact position of the target... and I want it to be a 3rd person view.
Try doing something like this- make the camera orbit around a central object, and then put the 'follow' script on the central object (so that it follows the position of the player). So, use a simple orbit camera script (just borrow the one from standard assets), and make it target an empty that is following the position only of the player.
For funsies, try making the player input relative to the direction of the camera!
Your answer
Follow this Question
Related Questions
My Character Controller Is Warping Randomly (/w video) 0 Answers
Character controller collision 2 Answers
Player object: capsule with Character Controller goes through the floor randomly 0 Answers
Help in Javascript destroying Objects using character controllers and conditionals 1 Answer
Delayed Collisions Bug (includes video demonstration) 1 Answer