• 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 /
avatar image
0
Question by JuanseCoello · Apr 02, 2014 at 01:04 AM · collisionraycastraycasthitslopes

How to check if a raycasthit detects an angle less than 45 degrees and touches the ground

I have this script:

 void IsGrounded()
 {

     RaycastHit hitInfo;
     if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
     {
         if (hitInfo.normal.y > notSlopes)
         isgrounded = true;

     }

     else isgrounded = false;
 }

Where notslopes stands for slopes that are not bigger than 45 degrees and to check if collision with the floor to restrict gravity when the character climbs on slopes.

How can I fix this, cause it is not working.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kavorka · Apr 02, 2014 at 01:18 AM

Change row 7 to:

if (hitInfo.normal.y < .707f)

then isgrounded will be true if you are standing on a slope steeper than 45 degrees.

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 Kavorka · Apr 02, 2014 at 01:23 AM 0
Share

[CODE] void IsGrounded() {

 RaycastHit hitInfo;
 if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
 {
     if (hitInfo.normal.y < .707f)
         angleLT45 = true;
     else
         angleLT45 = false;

     if (hitInfo.distance > 1f) // Change 1f to what you need
         isgrounded = false;
     else
         isgrounded = true; 
 }

} [/CODE]

avatar image
0

Answer by JuanseCoello · Apr 02, 2014 at 02:06 AM

It doesnt work, doesnt detect collision with the ground when applied gravity. here is my code:

using UnityEngine; using System.Collections;

      public class TP_Controller : MonoBehaviour 
       {
       public static Rigidbody Rigidbody2;
       public static TP_Controller Instance;

 RaycastHit hit;
 float dist;
 Vector3 dir;
 public bool isgrounded;
 public bool angleLT45;
 

 void Awake () // Debe quedarse en awake
 {

     Rigidbody2 = GetComponent("Rigidbody") as Rigidbody;
     Instance = this;

 }
 

 void Update () 
 {
     if (Camera.mainCamera == null)
         return;

     GetLocomotionInput();

     TP_Motor.Instance.UpdateMotor();

     dist = 10;
     dir = new Vector3(0,-1,0);

     Debug.DrawRay(transform.position, dir * dist, Color.green);
 }

 void IsGrounded()
 {

     RaycastHit hitInfo;
     if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
     {
         if (hitInfo.normal.y < .707f)
             angleLT45 = true;
         else
             angleLT45 = false;

         if (hitInfo.distance > .1f) // Change .1f to what you need
             isgrounded = false;
         else
             isgrounded = true;
     }

 } 

AND THEN THERE IS THIS CODE ON ANOTHER SCRIPT

          void ApplyGravity()
           {
            if (TP_Controller.Instance.isgrounded == true)
         return;

         if (MoveVector.y > -TerminalVelocity)
         MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
     if ((TP_Controller.Instance.isgrounded == false) && MoveVector.y < -1)
         MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
 }
Comment
Add comment · Show 2 · 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 JuanseCoello · Apr 02, 2014 at 02:16 AM 0
Share

$$anonymous$$aybe it doesnt check for anything to collide. It goes through.

avatar image JuanseCoello · Apr 02, 2014 at 02:20 AM 0
Share

What I want is that the raycast casts a ray of 1f, but when it collides to the floor it checks is grounded = true. That is it, maybe I didnt asked well the question.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast bullet collision problem 1 Answer

Raycast Not Drawing In Target Direction 0 Answers

How can I change this script? 0 Answers

Touch collision detection with a sprite 0 Answers

Raycast not Detecting Colliders 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges