- Home /
CapsuleCast not registering
I have a controller handler that I want to make capsule collision with, but my capsule cast debug isn't even launching?
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CapsuleCollider))]
public class PlayerControllerHandler : MonoBehaviour {
public LayerMask collisionMask;
private CapsuleCollider cc;
public int collisions;
void Start () {
cc = GetComponent <CapsuleCollider> ();
}
public void Move (Vector3 velocity) {
Collide (ref velocity);
transform.Translate (velocity);
}
void Collide (ref Vector3 velocity) {
Vector3 point1 = transform.TransformPoint (cc.center + Vector3.down * cc.radius);
Vector3 point2 = transform.TransformPoint (cc.center + Vector3.up * cc.radius);
Debug.Log (point1 + " " + point2);
float rayDistance = velocity.magnitude;
RaycastHit hit;
if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
{
Debug.Log ("Collision");
if (Mathf.Abs (transform.position.x - hit.point.x) < Mathf.Abs (velocity.x))
{
collisions ++;
velocity.x = transform.position.x - hit.point.x;
}
if (Mathf.Abs (transform.position.y - hit.point.y) < Mathf.Abs (velocity.y))
{
collisions ++;
velocity.y = transform.position.y - hit.point.y;
}
if (Mathf.Abs (transform.position.z - hit.point.z) < Mathf.Abs (velocity.z))
{
collisions ++;
velocity.z = transform.position.z - hit.point.z;
}
}
}
}
which debug? Is $$anonymous$$ove called somewhere? Is anything close enough?
@hexagonius my mistake, there is another script involved.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (PlayerControllerHandler))]
public class PlayerController : $$anonymous$$onoBehaviour {
public float gravity;
public float moveSpeed;
private Vector3 velocity;
private float currentGravity;
private PlayerControllerHandler pch;
void Start () {
pch = GetComponent <PlayerControllerHandler> ();
}
void Update () {
currentGravity = velocity.y;
velocity = Vector3.zero;
velocity.y = currentGravity;
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W))
{
velocity += transform.forward;
}
else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
{
velocity -= transform.right;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D))
{
velocity += transform.right;
}
else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S))
{
velocity -= transform.forward;
}
velocity.y += gravity * Time.deltaTime;
pch.$$anonymous$$ove (velocity * Time.deltaTime);
}
}
how far should the sweep go? To me the distance (velocity * Time.deltaTime) looks quite short.
@hexagonius I think that should be correct, because that's how far the character is predicted to go next frame.
@hexagonius I managed to somewhat fix the issue (in some ways) in the way that he no longer jitters higher and higher, but now jitters and the jitter dissolves. I'm not sure why the issue isn't completely gone.
New script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CapsuleCollider))]
public class PlayerControllerHandler : $$anonymous$$onoBehaviour {
public Layer$$anonymous$$ask collision$$anonymous$$ask;
private CapsuleCollider cc;
public int collisions;
public float skinWidth;
void Start () {
cc = GetComponent <CapsuleCollider> ();
}
public void $$anonymous$$ove (Vector3 velocity) {
Collide (ref velocity);
transform.Translate (velocity);
}
void Collide (ref Vector3 velocity) {
Vector3 point1 = transform.TransformPoint (cc.center + Vector3.down * cc.radius);
Vector3 point2 = transform.TransformPoint (cc.center + Vector3.up * cc.radius);
Debug.Log (point1 + " " + point2);
float rayDistance = velocity.magnitude + skinWidth;
float directionX = $$anonymous$$athf.Sign (velocity.x);
float directionY = $$anonymous$$athf.Sign (velocity.y);
float directionZ = $$anonymous$$athf.Sign (velocity.z);
RaycastHit hit;
Debug.DrawRay (point1, velocity, Color.red, rayDistance);
if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
{
Debug.Log ("Collision");
if ($$anonymous$$athf.Abs (transform.position.x - hit.point.x) < $$anonymous$$athf.Abs (velocity.x))
{
collisions ++;
velocity.x = ($$anonymous$$athf.Abs (hit.point.x - transform.position.x) - skinWidth) * directionX;
}
if ($$anonymous$$athf.Abs (transform.position.y - hit.point.y) < $$anonymous$$athf.Abs (velocity.y))
{
collisions ++;
velocity.y = ($$anonymous$$athf.Abs (hit.point.y - transform.position.y) - skinWidth) * directionY;
}
if ($$anonymous$$athf.Abs (transform.position.z - hit.point.z) < $$anonymous$$athf.Abs (velocity.z))
{
collisions ++;
velocity.z = ($$anonymous$$athf.Abs (hit.point.z - transform.position.z) - skinWidth) * directionZ;
}
}
}
}
Your answer
Follow this Question
Related Questions
Is there a way to set Unity to write message log when the calculation has Infinity or NaN? 3 Answers
Have my debug log saved to a .log file in build. 2 Answers
Debug.Log Override? 3 Answers
Reading the profiler log 0 Answers
Debug.Log is causing an Assert 2 Answers