• 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 awplays49 · Jul 27, 2015 at 08:40 PM · physics

Following tutorial (link included) where have I messed up?

https://www.youtube.com/watch?v=OBtaLCmJexk

the player is hovering about the skin width's length over the floor.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (BoxCollider2D))]
 public class PlayerController : MonoBehaviour {
     
     private BoxCollider2D colliderRef;
     private Bounds bounds;
     private float left, right;
     private float bottom, top;
     private Vector2 size;
     public int rayCountX, rayCountY;
     private float raySpacingX, raySpacingY;
     public float skinWidth;
     public LayerMask layerMask;
     public bool isGrounded;
     
     void Start () {    
         colliderRef = GetComponent <BoxCollider2D> ();
         UpdateRaycastBounds ();
         CalculateRaySpacing ();
     }
     
     void Update () {
         CheckForGround ();
     }
 
     void UpdateRaycastBounds () {
         bounds = colliderRef.bounds;
         left = bounds.min.x + skinWidth;
         right = bounds.max.x - skinWidth;
         bottom = bounds.min.y + skinWidth;
         top = bounds.max.y - skinWidth;
         size.x = bounds.size.x;
         size.y = bounds.size.y;
     }
     
     void CalculateRaySpacing () {
         raySpacingX = (size.x - skinWidth * 2) / (rayCountX - 1);
         raySpacingY = (size.y - skinWidth * 2) / (rayCountY - 1);
     }
     
     public void Move (Vector3 velocity) {
         UpdateRaycastBounds ();
         CalculateRaySpacing ();
         CollideVertically (ref velocity);
         CollideHorizontally (ref velocity);
         transform.Translate (velocity);
     }
     
     void CollideHorizontally (ref Vector3 velocity) {
         float direction = Mathf.Sign (velocity.x);
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         for (int x = 0; x < rayCountY; x ++)
         {
             Vector2 rayPos = Vector2.zero;
             if (direction == -1)
             {
                 rayPos = new Vector2 (left, bottom);
             }
             else
             {
                 rayPos = new Vector2 (right, bottom);
             }
             rayPos.y += raySpacingY * x;
             RaycastHit2D hit = Physics2D.Raycast (rayPos, Vector2.right * direction, rayLength, layerMask);
             Debug.Log (rayPos);
             Debug.DrawRay (rayPos, Vector2.right * direction * rayLength, Color.red);
             if (hit)
             {
                 velocity.x = (hit.distance - skinWidth) * direction;
                 rayLength = hit.distance;
             }
         }
     }
     
     void CollideVertically (ref Vector3 velocity) {
         float direction = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;
         for (int y = 0; y < rayCountX; y ++)
         {
             Vector2 rayPos = Vector2.zero;
             if (direction == -1)
             {
                 rayPos = new Vector2 (left, bottom);
             }
             else
             {
                 rayPos = new Vector2 (left, top);
             }
             rayPos.x += raySpacingX * y;
             RaycastHit2D hit = Physics2D.Raycast (rayPos, Vector2.up * direction, rayLength, layerMask);
             Debug.DrawRay (rayPos, Vector2.up * direction * rayLength, Color.red);
             if (hit)
             {
                 if (direction == 1)
                 {
                     velocity.y = 0;
                 }
                 else
                 {
                     velocity.y = (hit.distance - skinWidth) * direction;
                     rayLength = hit.distance;
                 }
             }
         }
     }
     
     void CheckForGround () {
         for (int r = 0; r < rayCountX; r ++)
         {
             Vector2 rayPos = new Vector2 (left, bottom);
             rayPos.x += raySpacingX * r;
             RaycastHit2D hit = Physics2D.Raycast (rayPos, Vector3.down, skinWidth, layerMask);
             if (hit)
             {
                 isGrounded = true;
             }
             else
             {
                 isGrounded = false;
             }
         }
     }
 }
 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

2D 360 degress platformer example needed 0 Answers

human character with ragdoll hair 2 Answers

How to make my Character run through walls with the use of the Character Controller component 1 Answer

Collider collides, triggers OnCollisionEnter, then bounces, before destroying itself 2 Answers

Vector3.Reflect problems 4 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