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;
}
}
}