Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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.

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

Fall collision force with help of raycast 0 Answers

Raycasting to detect which side of collider is hit 2 Answers

Detect RaycastHit on Character Controller 2 Answers

Raycast and timer to select and lock-on to targets 1 Answer

Raycast Not Drawing In Target Direction 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