Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by S4breK4t · Jul 09, 2015 at 12:52 PM · rotationcolliderraycasting

Help with rotating my collider and ray origins

In my platformer game, I want to code it so that the player rotates to the angle of the surface hes standing on. Ive been testing out rotating my box collider 2d manually for the moment, but the points where the rays are firing from are all messed up, eventhough they point in the right direction and do actually rotate. Picture reference:

alt text

alt text

As you can see there are my rays with no rotation and then how they fire with some rotation. Im not sure why this is happening, here's my code if any of you could tell me what stupid mistake I have made:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (BoxCollider2D))]
 public class RaycastController : MonoBehaviour {
     
     public LayerMask collisionMask;
     
     public const float skinWidth = .015f;
     public int horizontalRayCount = 4;
     public int verticalRayCount = 4;
     
     [HideInInspector]
     public float horizontalRaySpacing;
     [HideInInspector]
     public float verticalRaySpacing;
     
     [HideInInspector]
     public BoxCollider2D collider;
     public RaycastOrigins raycastOrigins;
     
     public virtual void Awake() {
 
         collider = GetComponent<BoxCollider2D> ();
     }
     
     public virtual void Start() {
 
         CalculateRaySpacing ();
     }
     
     public void UpdateRaycastOrigins() {
 
         Bounds bounds = collider.bounds;
         bounds.Expand (skinWidth * -2);
         
         raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
         raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
         raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
         raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
     }
     
     public void CalculateRaySpacing() {
 
         Bounds bounds = collider.bounds;
         bounds.Expand (skinWidth * -2);
         
         horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
         verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);
         
         horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
         verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
     }
     
     public struct RaycastOrigins {
 
         public Vector2 topLeft, topRight;
         public Vector2 bottomLeft, bottomRight;
     }
 }

 void HorizontalCollisions(ref Vector3 velocity) {
 
         float directionX = collisions.faceDir;
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         
         if (Mathf.Abs(velocity.x) < skinWidth) {
 
             rayLength = 2*skinWidth;
         }
         
         for (int i = 0; i < horizontalRayCount; i ++) {
 
             Vector3 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight;
             rayOrigin += transform.up * (horizontalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, transform.right * directionX, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, transform.right * directionX * rayLength,Color.white);
             
             if (hit) {
                 
                 velocity.x = (hit.distance - skinWidth) * directionX;
                 rayLength = hit.distance;
 
                 movementStopped = true;
 
                 collisions.left = directionX == -1;
                 collisions.right = directionX == 1;
 
             }
         }
     }
     
     void VerticalCollisions(ref Vector3 velocity) {
 
         float directionY = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;
         
         for (int i = 0; i < verticalRayCount; i ++) {
             
             Vector3 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
             rayOrigin += transform.right * (verticalRaySpacing * i + velocity.x);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, transform.up * directionY, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, transform.up * directionY * rayLength,Color.white);
             
             if (hit) {
 
                 if (hit.collider.tag == "Through") {
 
                     if (directionY == 1 || hit.distance == 0) {
 
                         continue;
                     }
 
                     if (collisions.fallingThroughPlatform) {
 
                         continue;
                     }
 
                     if (playerInput.y == -1) {
 
                         collisions.fallingThroughPlatform = true;
                         Invoke("ResetFallingThroughPlatform",.5f);
                         continue;
                     }
                 }
                 
                 velocity.y = (hit.distance - skinWidth) * directionY;
                 rayLength = hit.distance;
                 
                 collisions.below = directionY == -1;
                 collisions.above = directionY == 1;
             }
         }
         
 
     }

Thanks :)

normalrays.png (13.9 kB)
rotatedrays.png (18.4 kB)
Comment
Add comment · Show 1
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 S4breK4t · Jul 09, 2015 at 01:20 PM 0
Share

I think this is to do with how the bounding box rotates differently to the collider so the bounds are different, but I dont know how to fix that....

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Tilemap Collider wrong rotation 0 Answers

Non-rotating collider fixed to a rolling ball 1 Answer

Animal Rigidbody and Collider problem 0 Answers

SInce when?!?!? (Raycast issue) 2 Answers

Raycast Collider Tag not returning correct result 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