• 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
Question by ZeroAurora · Apr 26, 2014 at 04:58 AM · c#raycastraycasting

C# Using Ray Casting, but I fall down after contact on the side

So I currently know my code is not optimal in that I create extra Rays and such: rayX for horizontal movement, rayY for vertical movement, and ray for angled movement. I several: "Scene::raycastClosestShape: The maximum distance must be greater than zero!" errors previously and found a means of fixing them with this. But I now have an error such that when I collide with objects from the side (left/right) of my character, I will fall downwards, even if I'm on ground. This results in my player dropping through the level.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(BoxCollider))]
 public class PlayerPhysics : MonoBehaviour {
     public LayerMask collisionMask;
 
     private new BoxCollider collider;
     private Vector3 s;
     private Vector3 c;
 
     private Vector3 originalSize;
     private Vector3 originalCenter;
     private float colliderScale;
 
     private int collisionDivisionsX = 3;
     private int collisionDivisionsY = 10;
 
     private float skin = .005f;
 
     [HideInInspector]
     public bool grounded;
     [HideInInspector]
     public bool movementStopped;
 
     Ray rayX;
     Ray rayY;
     Ray ray;
     RaycastHit $$anonymous$$tX;
     RaycastHit $$anonymous$$tY;
 
     void Start() {
         collider = GetComponent<BoxCollider> ();
         colliderScale = transform.localScale.x;
 
         originalSize = collider.size;
         originalCenter = collider.center;
 
         SetCollider (originalSize, originalCenter);
     }
 
     public void Move(Vector2 moveAmount) {
 
         float deltaY = moveAmount.y;
         float deltaX = moveAmount.x;
         Vector2 p = transform.position;
 
         //Vertical Check
         grounded = false;
         for(int i = 0; i < collisionDivisionsX; i++) {
             float dirY = Mathf.Sign (deltaY);
             float x2 = (p.x + c.x - s.x/2) + s.x/(collisionDivisionsX-1) * i; //left, center and right-most points of the collider
             float y2 = p.y + c.y + s.y/2 * dirY; //bottom of collider
             
             rayY = new Ray(new Vector2(x2,y2), new Vector2(0,dirY));
             Debug.DrawRay(rayY.origin, rayY.direction);
             //Vertical Collisions
             bool moveVert = !Mathf.Approximately(deltaY, 0.0f);
             if (moveVert && Physics.Raycast(rayY,out $$anonymous$$tY,Mathf.Abs(deltaY) * skin,collisionMask)) {
                 //get Distance between player and ground
                 float dst = Vector3.Distance(rayY.origin, $$anonymous$$tY.point);
                 
                 //Stop player's downwards movement after coming wit$$anonymous$$ng skin width of a collider
                 if(dst > skin) {
                     deltaY = -dst + skin;
                 }
                 else {
                     deltaY = 0;
                 }
                 grounded = true;
                 break;
             }
         }
 
         //Horizontal Check
         movementStopped = false;
         for(int i = 0; i < collisionDivisionsY; i++) {
             float dirX = Mathf.Sign (deltaX);
             float x = p.x + c.x + s.x/2 * dirX; //left, center and right-most points of the collider
             float y = (p.y + c.y - s.y/2) + s.y/(collisionDivisionsY-1) * i; //bottom of collider
 
             rayX = new Ray(new Vector2(x,y), new Vector2(dirX, 0));
             Debug.DrawRay(rayX.origin, rayX.direction);
             //Horizontal Collisions
             bool moveHorz = !Mathf.Approximately(deltaX, 0.0f);
             if (moveHorz && Physics.Raycast(rayX,out $$anonymous$$tX,Mathf.Abs(deltaX) + skin,collisionMask)) {
                 //get Distance between player and ground
                 float dst = Vector3.Distance(rayX.origin, $$anonymous$$tX.point);
                 
                 //Stop player's downwards movement after coming wit$$anonymous$$ng skin width of a collider
                 if(dst > skin) {
                     deltaX = -dst + skin;
                 }
                 else {
                     deltaX = 0;
                 }
                 movementStopped = true;
                 break;
             }
         }
 
 
         if( !grounded && !movementStopped) {
             Vector3 playerDir = new Vector3 (deltaX, deltaY);
             Vector3 o = new Vector3 (p.x + c.x + s.x / 2 * Mathf.Sign(deltaX), p.y + c.y + s.y / 2 * Mathf.Sign(deltaY));
             ray = new Ray (o, playerDir.normalized);
 
             if (Physics.Raycast (ray,Mathf.Sqrt (deltaX * deltaX + deltaY * deltaY), collisionMask)) {
                 grounded = true;
                 deltaY = 0;
                 }
         }
 
         Vector2 finalTransform = new Vector2(deltaX, deltaY);
 
         transform.Translate (finalTransform);
     }
 
     //set collider
     public void SetCollider(Vector3 size, Vector3 centre) {
         collider.size = size;
         collider.center = centre;
 
         s = size * colliderScale;
         c = centre * colliderScale;
     }
 
     public void ResetCollider() {
         SetCollider (originalSize, originalCenter);
     }
 
 }


Comment

People who like this

0 Show 0
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

20 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

Related Questions

Raycast function doesn't work. 1 Answer

[C# / Unity] Raycast Ignoring Self / Offsetting Instantiations? 0 Answers

[C#] UNET Client raycast not the same as Server raycast 2 Answers

Multiple Cars not working 1 Answer

How do I single out GameObject being detected by Raycast in a destroy / respawn system 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