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
1
Question by TH3HCK3ABL3 · Jul 26, 2013 at 03:48 AM · terraincollide

My character will fall through the floor with this script:...

My character will fall through the floor with this script:

 {
     public float moveSpeed = 6.0f; // Movement speed
     public float turnSpeed = 90.0f; // Turning speed in degrees per second
     public float gravity = 10.0f; // Gravity acceleration
     public float jumpSpeed = 10.0f;
     public float jumpRange = 10.0f;
  
     float distGround;
     float lerpSpeed = 10.0f; // Smoothing speed
     float deltaGround = 0.2f; // Character is grounded up to the distance
     float vertSpeed = 0.0f; // The current speed of the Vertical Jump
  
     bool isGrounded;
     bool jumping = false; // Flag for jumping to the wall
  
     Vector3 surfaceNormal = new Vector3(1,2,3);
     Vector3 myNormal = Vector3.forward;
     Vector3 myForward;
  
     Ray ray;
     RaycastHit hit;
  
     Quaternion targetRot;
  
     // Use this for initialization
     void Start () 
     {
        myNormal = transform.up; // Normal starts as the up direction of the character
        rigidbody.freezeRotation = true; // Disables the rotation via physics
        distGround = collider.bounds.extents.y - collider.bounds.center.y; // Distance from transform.posistion to the ground
     }
  
     void FixedUpdate() 
     {
        rigidbody.AddForce (-gravity * rigidbody.mass * myNormal); // Apply constant weight force according to character normal
     }
  
     // Update is called once per frame
     void Update () 
     {
        // Jump Code - Jump to wall or simple Jump
        if (jumping) 
          return;
  
        if (Input.GetButtonDown ("Jump")) 
        { // If the player is pressing jump...
          ray = new Ray(transform.position, transform.forward);
             if (Physics.Raycast(ray, out hit, jumpRange)) 
          { // Checks to see if there is a wall
  
           //THERE IS NO METHOD FOR JUMPTOWALL!
           //JumpToWall(hit.point, hit.normal); // Yes Jump to the wall
          } 
        }
  
        else 
        {
          if(isGrounded) 
          { 
           //no; if grounded, jump up
           rigidbody.velocity += jumpSpeed * myNormal;
          }
        }
  
        // Movement Code - Turn left/right with Horizontal Axis
        transform.Rotate(0, Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime, 0); // Update the surface noraml and isGrounded
        ray = new Ray(transform.position, -myNormal); // Cast a ray downwards
  
        if (Physics.Raycast(ray, out hit)) 
        {
          isGrounded = hit.distance <= distGround + deltaGround;
          surfaceNormal = hit.normal;
        } 
  
        else 
        {
          isGrounded = false;
          surfaceNormal = Vector3.up; // Assumes that the noraml is up to avoid some weird shit
        }
  
        myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
        myForward = Vector3.Cross (transform.right, myNormal); // Finds the forwards direction with my normal
        targetRot = Quaternion.LookRotation (myForward, myNormal); // Alligns the character to the new myNormal whilst keep the same forward Direction
        transform.rotation = Quaternion.Lerp (transform.rotation, targetRot, lerpSpeed * Time.deltaTime);
        transform.Translate (0, 0, Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime);
     }
 }

I don't know why he is falling through the floor, I have tried adding rigidbody to the floor, a collider, making entirely new terrain, and moving the character upwards so that it will fall and have time to update and collide. Help please?

Comment
Add comment · Show 5
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 Linus · Jul 26, 2013 at 04:09 AM 0
Share

Does it fall through if you do not have this script disabled? If you are using a mesh collider for the floor it needs to be static. I now you said terrain, but you also say floor, so just be sure.

This page might help http://docs.unity3d.com/Documentation/Components/class-SphereCollider.html Particularly the table at the bottom.

avatar image TH3HCK3ABL3 · Jul 26, 2013 at 04:12 AM 0
Share

It doesn't have gravity if the script isn't enabled. my previous script worked just fine, but I wanted new movement abilities. And It does have a mesh. I don't know if I understand what you mean by static. It doesn't move at all as far as I can tell. I have a terrain collider, mesh and rigidbody body on the map, which will not work with the script above for some reason.

avatar image TH3HCK3ABL3 · Jul 26, 2013 at 04:16 AM 0
Share

I have a comment listed by pickle chips in my email that doesn't seem to be properly showing up here. His/her comment was "Did you make sure he's placed entirely above the floor when you start it?" and to answer his/her question: Yes, The character is 5 meters above the entire level.

avatar image Linus · Jul 26, 2013 at 04:21 AM 0
Share

The floor/terrain/ground should not have a rigidbody. Just above the Transform in the inspector there is a little tick box that say "static". In 99% of cases it should be turned on for the ground.

Also if you are using the character controler, you can just tick 'use gravity' on the rigidbody. And not use rigidbody.AddForce (-gravity * rigidbody.mass * myNormal); // Apply constant weight force according to character normal

Hard to tell, but most often when I have issues with colliders its something like this that causes the problem.

If you are certain its your script, try to comment out parts until you have found the cause.

avatar image TH3HCK3ABL3 · Jul 26, 2013 at 04:44 AM 0
Share

I've made the level static, I have commented out my gravity, and vice versa. I have deter$$anonymous$$ed that I can move while I am falling, and I can look. I am just not colliding with my level! Very frustrating.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Linus · Jul 26, 2013 at 05:13 AM

After our talk above I tested the script.

The script works if you do not use character controller but only a collider and rigidbody. It worked with or without gravity enabled.

I should have started Unity earlier, character controller and rigidbody does not work well together.

Hope you get it working

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 TH3HCK3ABL3 · Jul 26, 2013 at 05:49 AM 0
Share

Ahhhhh I see, at first I was confused because it wouldn't let me run it after I removed the character controller. Haha it was because I required the character controller component. After removing that, and adding a mesh collider to my level, it functions perfectly. Almost... except I cannot jump... But alas that is a question for another day! Thank you very much for your help! I would vote up but I have no reputation! Thank you so much again!

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

17 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

Related Questions

Multiple Cars not working 1 Answer

How to make blocks collide - help fast please 1 Answer

Object falling through terrain with script 2 Answers

fire script 1 Answer

RayCast Shooting not working 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