• 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 /
  • Help Room /
avatar image
0
Question by xyxx · Nov 21, 2016 at 06:51 AM · c#collisionphysicsgravitycollision detection

Object refuses to collide

Im trying to implement bunnyhopping as a mechanic in my game, but collisions dont work if I use MoveGround as the movement function, but collision work if I use the CharacterController Move function. The capsule the script is attached to just falls through a cube I have as a floor.

If anyone could help solve this that would be fantastic

 public class PlayerScript : MonoBehaviour 
 {
 
     public float moveSpeed = 0;
     public float jumpHeight = 0;
     public float gravity = 0;
 
     public float friction = 0;
     public float groundAccelerate = 0;
     public float airAccelerate = 0;
 
     public float maxVelocityGround = 0;
     public float maxVelocityAir = 0;
 
     CharacterController player;
     Vector3 currPos;
     Vector3 nextPos;
 
     float distance;
     int platLayer;
 
     void Start () 
     {
         player = GetComponent<CharacterController>();
         currPos = transform.position;
         distance = player.radius + 0.2f;
     }
     
     void Update () 
     {
 
         if(player.isGrounded)
         {
             nextPos = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             nextPos = transform.TransformDirection(nextPos);
             nextPos *= moveSpeed;
             if(Input.GetKey(KeyCode.Space))
             {
                 nextPos.y = jumpHeight;
             }
         }
 
         transform.position -= Vector3.up * gravity * Time.deltaTime;
         transform.position += MoveGround(nextPos, currPos) * Time.deltaTime;
 
         /*  Collision Checking  */
         RaycastHit hit;
 
         Vector3 p1 = transform.position + Vector3.up * 0.25f;
         Vector3 p2 = p1 + Vector3.up * player.height;
 
         for(int i=0; i<360; i+=36)
         {
             if(Physics.CapsuleCast(p1,p2,0,new Vector3(Mathf.Cos(i), 0, Mathf.Sin(i)), out hit, distance, 1 << platLayer))
             {
                 player.Move(hit.normal*(distance-hit.distance));
             }
         }
     
     }
 
     private Vector3 Accel(Vector3 accelDir, Vector3 prevVelocity, float accelerate, float mVel)
     {
         float projVel = Vector3.Dot(prevVelocity, accelDir);
         float accelVel = accelerate * Time.fixedDeltaTime;
 
         if(projVel + accelVel > mVel){ accelVel = mVel - projVel; }
 
         return prevVelocity + accelDir * accelVel;
     }
 
     private Vector3 MoveGround(Vector3 accelDir, Vector3 prevVelocity)
     {
         float speed = prevVelocity.magnitude;
         if (speed != 0)
         {
             float drop = speed * friction * Time.fixedDeltaTime;
             prevVelocity *= Mathf.Max(speed - drop, 0) / speed;
         }
         return Accel(accelDir, prevVelocity, groundAccelerate, maxVelocityGround);
     }
 
     private Vector3 MoveAir(Vector3 accelDir, Vector3 prevVelocity)
     {
         return Accel(accelDir, prevVelocity, airAccelerate, maxVelocityAir);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

283 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 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 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

How To how ?? 2 Answers

Ignore collision based on position 1 Answer

How can I bind this script into the state of my camera? 2 Answers

Unity Annoyance with Physics Help C# Scripting Problem Momentum Issue Please Help 0 Answers

WheelColliders Slip without end (with planet gravity, custom terrain mesh) 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