Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by mandismash · May 02, 2012 at 12:55 AM · collisioncontrollercharacter controlleroncollisionenterflight

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mandismash · May 02, 2012 at 03:01 AM 0
Share

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! =)

avatar image syclamoth · May 02, 2012 at 03:07 AM 0
Share

$$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.

avatar image mandismash · May 02, 2012 at 03:23 AM 1
Share

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.

avatar image syclamoth · May 02, 2012 at 03:26 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges