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 /
avatar image
0
Question by DocBroc · Sep 02, 2014 at 11:11 PM · collisioncharactercontroller

Problem with character controller and collisions

For some reason, after saving a quitting my project then reloading it, my player [complete with CharacterController script] keeps falling through the floor. What happened, how do I fix it and how do I prevent this from happening again?

I know that similar questions have been answered, but I havetn't been able to find one that helps with this problem..

EDIT** I get an error that says "NullReferenceException: Object reference not set to an instance of an object"

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
 
     public float moveSpeed = 10.0f;
     public float mouseSensitivity = 5.0f;
     public float jumpSpeed = 7.0f;
 
     public float rotUpDown = 0.0f;
     public float headTiltRange = 60.0f;
 
     float vertVelocity = 0;  
 
     CharacterController charcont;
     // Use this for initialization
     void Start () {
         Screen.lockCursor = true;
         CharacterController charcont = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
         //Rotation
         float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
         transform.Rotate (0, rotLeftRight, 0);
 
         rotUpDown -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
         rotUpDown = Mathf.Clamp (rotUpDown, -headTiltRange, headTiltRange);
         Camera.main.transform.localRotation = Quaternion.Euler (rotUpDown, 0, 0);
 
         //Movement
             float fwdSpeed = Input.GetAxis ("Vertical") * moveSpeed;
             float sideSpeed = Input.GetAxis ("Horizontal") * moveSpeed;
 
             //Gravity
             vertVelocity += Physics.gravity.y * Time.deltaTime;
             Vector3 speed = new Vector3 (sideSpeed, vertVelocity, fwdSpeed);
 
             //Jumping
         if (charcont.isGrounded && Input.GetButton ("Jump")) {
                 //vertVelocity = jumpSpeed;
             }
 
             speed = transform.rotation * speed;
 
         charcont.Move (speed * Time.deltaTime);
     }
 }
 


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
0

Answer by aldonaletto · Sep 02, 2014 at 11:36 PM

CharacterControllers have this bad habit of passing through the terrain: if the CharacterController penetrates even a little into the ground, it falls like a rock. Are you moving the character transform with Translate? Or could the terrain being shifted up by any other part of the code? If none of these, try to place the character at some height above the terrain (0.5 unit, for instance) in the Editor, so that it falls naturally to the ground when the game starts. About the Null Reference error: read the error message to find where it's occurring - maybe this error has nothing to do with the falling character.

Comment
Add comment · Show 3 · 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 DocBroc · Sep 02, 2014 at 11:43 PM 0
Share

The thing is, everything worked 100% before I saved and quit. I moved the character to 20 units above the terrain to be sure it wasn't touching, and it still fell through. Also, I never had that NullReferenceError before I saved and quit as well.

avatar image aldonaletto · Sep 02, 2014 at 11:46 PM 0
Share

Find where the error is occurring - which script, which line etc. - and post the relevant code

avatar image DocBroc · Sep 03, 2014 at 12:44 AM 0
Share

if (charcont.isGrounded && Input.GetButton ("Jump")) { vertVelocity = jumpSpeed; }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnCollisionEnter Push Object Problem 0 Answers

Collision Detection in Capsule Collider 0 Answers

Mesh separating from CharacterController 0 Answers

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

CharacterController collision detection with pendulum like object 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