Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Nov 06, 2014 at 02:31 PM by unimechanic for the following reason:

Too subjective and argumentative

avatar image
0
Question by death · Nov 04, 2014 at 10:12 PM · c#collidersfor

Help with a collider script

Questions:

  1. In this script, why are we doing the raycasts and collider size and center initialization under the for loop?

  2. For float y, what does " s.y/2 * dir; " actually mean? Similarly for float x in the left and right collisions

Note: DeltaY is equal to my jump height -= gravity * Time.deltaTime

DeltaX is equal to the direction the player is going in and his speed.

 using UnityEngine;
 using System.Collections;
 
 
 [RequireComponent (typeof(BoxCollider))]
 public class PlayerPhysics : MonoBehaviour {
     
     public LayerMask collisionMask;
 
     private BoxCollider collider;
     private Vector3 s;
     private Vector3 c;
     
     private float skin = .005f;
     
     [HideInInspector]
     public bool grounded;
     [HideInInspector]
     public bool movementStopped;
     
     Ray ray;
     RaycastHit hit;
     
     void Start() {
         collider = GetComponent<BoxCollider>();
         s = collider.size;
         c = collider.center;
     }
 
     public void Move(Vector2 moveAmount) {
         
         float deltaY = moveAmount.y;
         float deltaX = moveAmount.x;
         Vector2 p = transform.position;
         
         // Check collisions above and below
         grounded = false;
         
         for (int i = 0; i<3; i ++) {
             float dir = Mathf.Sign(deltaY);
             float x = (p.x + c.x - s.x/2) + s.x/2 * i; // Left, centre and then rightmost point of collider
             float y = p.y + c.y + s.y/2 * dir; // Bottom of collider
             
             ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
             Debug.DrawRay(ray.origin,ray.direction);
             
             if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) + skin,collisionMask)) {
                 // Get Distance between player and ground
                 float dst = Vector3.Distance (ray.origin, hit.point);
                 
                 // Stop player's downwards movement after coming within skin width of a collider
                 if (dst > skin) {
                     deltaY = dst * dir - skin * dir;
                 }
                 else {
                     deltaY = 0;
                 }
                 
                 grounded = true;
                 
                 break;
                 
             }
         }
         
         
         // Check collisions left and right
         movementStopped = false;
         for (int i = 0; i<3; i ++) {
             float dir = Mathf.Sign(deltaX);
             float x = p.x + c.x + s.x/2 * dir;
             float y = p.y + c.y - s.y/2 + s.y/2 * i;
             
             ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
             Debug.DrawRay(ray.origin,ray.direction);
             
             if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {
                 // Get Distance between player and ground
                 float dst = Vector3.Distance (ray.origin, hit.point);
                 
                 // Stop player's downwards movement after coming within skin width of a collider
                 if (dst > skin) {
                     deltaX = dst * dir - skin * dir;
                 }
                 else {
                     deltaX = 0;
                 }
                 
                 movementStopped = true;
                 break;
                 
             }
         }
         
         
     
         
         Vector2 finalTransform = new Vector2(deltaX,deltaY);
         
         transform.Translate(finalTransform);
     }
     
 }
 

Comment
Add comment · Show 4
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 death · Nov 05, 2014 at 03:12 AM 0
Share

Anyone at all?

avatar image incorrect · Nov 05, 2014 at 05:16 AM 0
Share

Did you just copy-paste someone's code and ask us to figure out how it works?

avatar image HarshadK · Nov 05, 2014 at 05:53 AM 0
Share

That's correct @incorrect

avatar image death · Nov 05, 2014 at 12:16 PM 0
Share

@incorrect His tutorial coding is different from the code he actually gave us. He didn't explain much, he just told us what to do, which is why I'm here asking.

No need to be rancorous.

1 Reply

  • Sort: 
avatar image
0

Answer by unimechanic · Nov 06, 2014 at 02:31 PM

Please use the forums to discuss on source code.

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 Answers

Collision against conditions 1 Answer

Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 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