• 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 /
avatar image
0
Question by BluemanGreenworld · Jul 14, 2017 at 11:44 AM · scripting problemcollidercharactercontroller

Object with Character Controller falls through ground.

I'm currently working on controls atm, and I have my object with a cc and my script (no rigid body). Every time I run it, however, the character just falls through the floor. The gravity is handled in the script. I have tried adding rigid bodies to the floor, the character, tried adding separate colliders, etc. and nothing works. Here is my code in case it helps. And no, none of the colliders on the floors are triggers.

 using UnityEngine;
 using System.Collections;

 public class CharacterController : MonoBehaviour {


 float mouseVertical = 0f;
 float vertVelocity = 0f;
 [SerializeField]
 float speed;

 [SerializeField]
 float sensitivity;

 [SerializeField]
 GameObject cam;

 CharacterController cc;

 // Use this for initialization
 void Start () {
     cc = GetComponent<CharacterController>();
     
 }
 
 // Update is called once per frame
 void Update () {
     moving();
     cameraController();
 }

 void moving()
 {
     float mouseHorizontal = Input.GetAxis("Mouse X");

     cc.transform.Rotate(0, mouseHorizontal*sensitivity, 0);

     float moveZ = Input.GetAxis("Vertical");
     float moveX= Input.GetAxis("Horizontal");

     vertVelocity += Physics.gravity.y * Time.deltaTime;

     Vector3 movement = new Vector3(moveX* speed, vertVelocity, moveZ * speed);

     cc.Move(movement * Time.deltaTime);

 }

 void cameraController()
 {
     mouseVertical -= Input.GetAxis("Mouse Y") * sensitivity;

     mouseVertical = Mathf.Clamp(mouseVertical, -90, 80);

     cam.transform.localRotation = Quaternion.Euler(mouseVertical, 0, 0);

     Debug.Log(cam.transform.rotation.x);
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WideEyeNow · Jul 14, 2017 at 12:31 PM

I had the same problem, I had to use SimpleMove, instead of just Move, and actualy set it always moving, but with a speed of 0. but yes you need a rigidbody and character controller 'capsule' thing. And make sure capsule is just above floor just a tad.

Comment
Add comment · Show 2 · 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 BluemanGreenworld · Jul 14, 2017 at 09:12 PM 0
Share

A.) I know that the Character Controller collider is not starting in the floor. It is well above it. B.) I have already tried putting a Rigidbody on it, nothing happens.

avatar image WideEyeNow · Jul 14, 2017 at 10:59 PM 0
Share

Try putting this in the object:

 Vector3 move = new Vector3 (0, 0, 0); 
 gameObject.Simple$$anonymous$$ove (move * speed);

I know it sounds stupid, but just paste this in your update() and see if stops falling

avatar image
0

Answer by Bunny83 · Jul 15, 2017 at 12:07 AM

You need to cancel your vertical velocity when your character controller collides below with the ground. Currently your vertical velocity is increasing without any condition and any limit. So even when you hit the ground and may be stopped for a sec. the velocity gets bigger and bigger until you simply teleport through the ground.

The Move method returns a CollisionFlags value to indicate where it collided.

Comment
Add comment · Show 1 · 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 WideEyeNow · Jul 15, 2017 at 12:16 AM 0
Share

There's a problem with rigidbody + character controller and using $$anonymous$$ove. Simple$$anonymous$$ove has to be called. $$anonymous$$y code doesn't call velocity at all, it only called $$anonymous$$ove, and character fell through floor every time. I threw in the Simple$$anonymous$$ove, and everything worked fine. :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObjects glitching through other Objects when being held 0 Answers

Collision CharacterController vs CharacterController 2 Answers

How to use Scriptable Objects on Prefabs correctly 0 Answers

Box collider that triggers camera change 2 Answers

Water collider in script 0 Answers


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