• 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 · Jun 29, 2015 at 09:39 PM · charactercharacter motor

Character somewhat works when going up stairs

Hello

Recently I started making the character controller (completely from scratch) for my character. Basically all that the Player script does is sends a velocity to the Controller script every frame.

Controller code:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (BoxCollider2D))]
 public class PlayerController : MonoBehaviour {
     
     private BoxCollider2D colliderRef;
     private RaycastOrigins raycastOrigins;
     public float skinWidth;
     public int rayCountX = 4, rayCountY = 4;
     private float spacingX, spacingY;
     public LayerMask layerMask;
     
     struct RaycastOrigins {
         public Vector2 bottomLeft, bottomRight;
         public Vector2 topLeft, topRight;
     }
     
     void Start () {
         colliderRef = GetComponent <BoxCollider2D> ();
         CalculateRaySpacing ();
     }
     
     void UpdateRaycastBounds () {
         Bounds bounds = colliderRef.bounds;
         raycastOrigins.bottomLeft = new Vector2 (bounds.min.x + skinWidth, bounds.min.y + skinWidth);
         raycastOrigins.bottomRight = new Vector2 (bounds.max.x - skinWidth, bounds.min.y + skinWidth);
         raycastOrigins.topLeft = new Vector2 (bounds.min.x + skinWidth, bounds.max.y - skinWidth);
         raycastOrigins.topRight = new Vector2 (bounds.max.x - skinWidth, bounds.max.y - skinWidth);
     }
     
     void CalculateRaySpacing () {
         Bounds bounds = colliderRef.bounds;
         rayCountX = Mathf.Clamp (rayCountX, 2, 8);
         rayCountY = Mathf.Clamp (rayCountY, 2, 16);
         spacingX = (bounds.size.x - skinWidth) / (rayCountY - 1);
         spacingY = (bounds.size.y - skinWidth) / (rayCountX - 1);
     }
     
     public void Move (Vector3 velocity) {
         UpdateRaycastBounds ();
         CollideVertically (ref velocity);
         CollideHorizontally (ref velocity);
         transform.Translate (velocity);
     }
     
     void CollideVertically (ref Vector3 velocity) {
         float directionY = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;
         for (int r = 0; r < rayCountY; r ++)
         {
             Vector2 rayOrigin = Vector2.zero;
             if (directionY == -1)
             {
                 rayOrigin = raycastOrigins.bottomLeft;
             }    
             else if (directionY == 1)
             {
                 rayOrigin = raycastOrigins.topLeft;
             }
             rayOrigin += Vector2.right * (spacingX * r + velocity.x);
             RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.up * directionY, rayLength, layerMask);
             if (hit == true)
             {
                 velocity.y = (hit.distance - skinWidth) * directionY;
                 rayLength = hit.distance;
             }
         }
         for (int r = 0; r < rayCountY; r ++)
         {
             Vector2 rayOrigin = raycastOrigins.bottomLeft;
             rayOrigin += Vector2.right * (spacingX * r + velocity.x);
             RaycastHit2D groundCheck = Physics2D.Raycast (rayOrigin, Vector2.down, skinWidth, layerMask);
             Debug.DrawRay (rayOrigin, Vector2.down * skinWidth, Color.blue, layerMask);
             if (groundCheck == true)
             {
                 WalkUpBlocks ();
             }
         }
     }
     
     void CollideHorizontally (ref Vector3 velocity) {
         float directionX = Mathf.Sign (velocity.x);
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         for (int r = 0; r < rayCountX; r ++)
         {
             Vector2 rayOrigin = Vector2.zero;
             if (directionX == -1)
             {
                 rayOrigin = raycastOrigins.bottomLeft;
             }    
             else if (directionX == 1)
             {
                 rayOrigin = raycastOrigins.bottomRight;
             }
             rayOrigin += Vector2.up * (spacingY * r);
             RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.right * directionX, rayLength, layerMask);
             if (hit == true)
             {
                 velocity.x = (hit.distance - skinWidth) * directionX;
                 rayLength = hit.distance;
             }
         }        
     }
     
     void WalkUpBlocks () {
         Bounds bounds = colliderRef.bounds;
         RaycastHit2D hitLeft = Physics2D.Raycast (new Vector2 (bounds.min.x - 0.125f, bounds.min.y + 0.875f), Vector2.down, 0.75f, layerMask);
         Debug.DrawRay (new Vector2 (bounds.min.x - 0.125f, bounds.min.y + 0.875f), Vector2.down * 0.75f, Color.red, layerMask);
         if (hitLeft == true)
         {
             if (hitLeft.distance == 0.75f)
             {
                 if (Input.GetKey (KeyCode.A))
                 {    
                     transform.position += Vector3.up / 4;
                     transform.position += Vector3.left / 4;    
                 }
             }
         }
         RaycastHit2D hitRight = Physics2D.Raycast (new Vector2 (bounds.max.x + 0.125f, bounds.min.y + 0.875f), Vector2.down, 0.75f, layerMask);
         Debug.DrawRay (new Vector2 (bounds.max.x + 0.125f, bounds.min.y + 0.875f), Vector2.down * 0.75f, Color.red, layerMask);
         if (hitRight == true)
         {
             if (hitRight.distance == 0.75f)
             {
                 if (Input.GetKey (KeyCode.D))
                 {    
                     transform.position += Vector3.up / 4;
                     transform.position += Vector3.right / 4;    
                 }
             }
         }
     }
 }

The blocks are scaled at 0.25 of a unit, and the player is 0.5x1 units.

The player only walks up blocks when going to the right, but it is sort of jittery, and the player also doesn't go up on left blocks.

I hope this can be sorted out. Cheers!

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

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

How to change gravity direction on Character Motor 0 Answers

Character x-speed drops to fast 0 Answers

How can I change the character beign controled and the camera with a button? 1 Answer

How do I use animations from one Mixamo character on another? 2 Answers

Faces of mesh getting deleted on import (Cinema 4D) 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges