Raycast doesn't work properly
Hi there, I'm having the issue for some days now, where my Raycast sometimes works, sometimes doesn't without any apparent reason. I want to slide my character down a terrain basically, so I'm using raycast to move it onto the surface constantly. However, for some strange reason, it suddenly decides that it doesn't see my terrain anymore, despite the fact that the ray should collide with it, so it just falls through the floor.
Here is a gif: https://drive.google.com/file/d/0B3j5DNuNqtGMb0psNEJKQVlIVGs/view?usp=sharing
using UnityEngine;
using System.Collections;
public class SlidingTest : MonoBehaviour {
[SerializeField]
Transform character;
[SerializeField]
float slideSpeed;
[SerializeField]
float climbAmount;
[SerializeField]
[Tooltip("The distance where we simply say we climbed close enough to our original position")]
float climbErrorMargin;
[SerializeField]
[Tooltip("The angle that triggers a sliding animation (ground tilt)")]
float slideAngle;
[SerializeField]
[Tooltip("A bit of offset on the raycast so it doesn't go through the floor")]
float raycastYOffset;
[SerializeField]
[Tooltip("Max distance that raycast checks (no teleporting from midair)")]
float maxCheckDistance;
[HideInInspector]
public Vector3 slideDirection = Vector3.zero;
[HideInInspector]
public float autoBreakSlide = 0f;
Vector3 characterOriginalLocalPos;
Coroutine doSlide;
Vector3 slideNewPos = Vector3.zero;
Vector3 slideSurfaceNormal = Vector3.zero;
bool firstClimb = true;
public void StartSliding(){
Debug.Log ("Started sliding");
character.GetComponent<Animator>().SetBool ("sliding", true);
GetComponent<FirstPersonController> ().enabled = false;
if(doSlide == null){
doSlide = StartCoroutine (DoSlide());
}
}
public void StopSliding(){
Debug.Log ("Stopped sliding");
if(doSlide != null){
StopCoroutine (doSlide);
print ("Stopped coroutine");
}
//character.GetComponent<Animator>().SetBool ("sliding", false);
firstClimb = false;
//GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
}
IEnumerator DoSlide(){
while (true) {
if(autoBreakSlide > 0 && (character.position - transform.position).magnitude > autoBreakSlide){
ResetPlayer (true);
yield break;
}
RaycastSlide ();
slideNewPos += slideDirection * slideSpeed * Time.deltaTime;
//print (slideNewPos);
character.position = slideNewPos;
character.rotation = Quaternion.FromToRotation (Vector3.up, slideSurfaceNormal);
character.Rotate (0, 90, 0);
slideDirection = character.forward;
yield return null;
}
}
void Start(){
characterOriginalLocalPos = character.localPosition;
}
void Update(){
if(Input.GetKeyDown(KeyCode.B)){
StopSliding ();
if(!firstClimb){
print ((character.position - transform.position).magnitude);
if ((character.position - transform.position).magnitude < climbErrorMargin) {
ResetPlayer (false);
} else {
RaycastSlide ();
slideNewPos -= character.transform.forward * climbAmount;
slideNewPos.y += 0.05f;
character.position = slideNewPos;
character.rotation = Quaternion.FromToRotation (Vector3.up, slideSurfaceNormal);
character.Rotate (0, 90, 0);
}
}
}
if (RaycastSlide ()) {
if(Vector3.Angle (Vector3.up, slideSurfaceNormal) >= slideAngle){
StartSliding ();
}
else {
StopSliding ();
ResetPlayer (true);
}
}
}
void ResetPlayer(bool toNewPos){
if (toNewPos) {
transform.position = character.position;
}
character.localPosition = characterOriginalLocalPos;
character.localRotation = Quaternion.Euler (Vector3.zero);
character.GetComponent<Animator>().SetBool ("sliding", false);
GetComponent<FirstPersonController> ().enabled = true;
}
bool RaycastSlide(){
RaycastHit hit;
Ray ray = new Ray (new Vector3(character.position.x,character.position.y+raycastYOffset,character.position.z),-Vector3.up);
if (Physics.Raycast (ray, out hit,maxCheckDistance)) {
//Debug.Log ("Hit: "+hit.transform.name+" with point: "+hit.point);
slideNewPos = hit.point;
if (character.gameObject.GetComponentInChildren<MeshFilter> () != null) {
Bounds bounds = character.gameObject.GetComponent<MeshFilter> ().sharedMesh.bounds;
slideNewPos.y += bounds.extents.y;
}
slideSurfaceNormal = hit.normal;
return true;
} else {
//Apply gravity if we dont have a surface (later on switch animation)
slideNewPos += Physics.gravity*Time.deltaTime;
return false;
}
}
}
Answer by bonzaiferroni · May 25, 2016 at 02:20 AM
Since you are constantly shooting a raycast down to see if there is terrain, and then moving your character along the terrain using
character.position = slideNewPos;
eventually your character moves through the terrain and the raycast you are shooting down doesn't hit terrain and so this line causes your character to drop:
slideNewPos += Physics.gravity*Time.deltaTime;
It seems with this code you are trying to recreate some of the basic functions of colliders and rigidbodies. Is there a reason why you are avoiding those? I think you might find it a lot simpler to work with.
I tried making the raycast shoot above the "next position", so basically the raycast was doing the sliding, but got the same results. Also, I tried using a stupidly big yOffset, like 2 units, so it was well above ground when it fell through.
I'm not sure what you mean by Rigidbody and colliders. Since this mesh is basically part of the player, that already has a character controller, I didn't really want to use any fancy physics components. However, feel free to point me in the right direction if I missed something. I'm always eager to get a simpler solution :)
Your answer
Follow this Question
Related Questions
Raycast on moving object? 2 Answers
Raycasting Stopped Working all of a sudden 0 Answers
Raycast shooting, child collider call and detection 0 Answers
Help with Raycast Script 0 Answers
Calculating distance is not working 1 Answer