Hi there. My collision is not working with 3d objects. maybe you can help me. Code (CSharp): using UnityEngine; using System.Collections; [RequireComponent (typeof (BoxCollider))] public class ControllerSQ : MonoBehaviour { public LayerMask CollisionMask; const float SkinWidth = .015f; public int HorizontalRayCount = 4; public int VerticalRayCount = 4; private float _horizontalRaySpacing; private float _verticalRaySpacing; private BoxCollider _collider; private RaycastOrigins _raycastOrigins; void Start() { _collider = GetComponent<BoxCollider> (); CalculateRaySpacing (); } public void Move(Vector3 velocity) { UpdateRaycastOrigins (); if (velocity.x != 0) { HorizontalCollisions (ref velocity); } if (velocity.y != 0) { VerticalCollisions (ref velocity); } transform.Translate (velocity); } void HorizontalCollisions(ref Vector3 velocity) { float directionX = Mathf.Sign (velocity.x); float rayLength = Mathf.Abs (velocity.x) + SkinWidth; for (int i = 0; i < HorizontalRayCount; i ++) { Vector2 rayOrigin = (directionX == -1)?_raycastOrigins.bottomLeft:_raycastOrigins.bottomRight; rayOrigin += Vector2.up * (_horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, CollisionMask); Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength,Color.red); if (hit) { velocity.x = (hit.distance - SkinWidth) * directionX; rayLength = hit.distance; } } } 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 ++) { Vector2 rayOrigin = (directionY == -1)?_raycastOrigins.bottomLeft:_raycastOrigins.topLeft; rayOrigin += Vector2.right * (_verticalRaySpacing * i + velocity.x); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, CollisionMask); Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength,Color.red); if (hit) { velocity.y = (hit.distance - SkinWidth) * directionY; rayLength = hit.distance; } } } 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); } 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); } struct RaycastOrigins { public Vector2 topLeft, topRight; public Vector2 bottomLeft, bottomRight; } } Code (CSharp): using UnityEngine; using System.Collections; [RequireComponent (typeof (ControllerSQ))] public class Player : MonoBehaviour { float moveSpeed = 6; float gravity = -20; Vector3 velocity; ControllerSQ controller; void Start() { controller = GetComponent<ControllerSQ> (); } void Update() { Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical")); velocity.x = input.x * moveSpeed; velocity.y += gravity * Time.deltaTime; controller.Move (velocity * Time.deltaTime); } }
Out of curiosity - is there a specific reason why you're reimplementing collision checks, gravity etc. with collider objects (or actually - at all)? In that scenario you might want to use a mesh, to save on all the Physics calls that Unity will do anyway when you create a collider.
I am making squirrel movement. so i need it to move perpendicular to ground. So I thought that would be a good idea check collision allways. Because I need to check when im on the ground, which angle i am moving. But maybe I will try mesh also. Good idea. thanks