Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by gogo199432 · May 25, 2016 at 01:44 AM · raycastterraincollision detectionground detection

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;
         }
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image gogo199432 · May 25, 2016 at 11:44 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RayCast StandartProjectAssets Ethan prefab problem. 2 Answers

Bugging ragdoll 1 Answer

Edge collider BoxCast issue 0 Answers

What is the best way to do a 2D Ground Check? 0 Answers

Raycast Information 0 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