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 SVGK · May 09, 2014 at 06:24 PM · raycastraycasthitgrounded

How to detect what I'm standing on?.

Hello, I'm building a character controller using a rigidbody and forces to move, and a raycast to check if we're grounded.

However, moving platforms are the bane of my existence, I need a way to copy the velocity of the platform that is moving when I'm standing on it.

However, the trouble comes in when I'm trying to detect what I'm standing on exactly, and how to get at variables on the stood object.

This is my script at the moment:

 using UnityEngine;
 using System.Collections;
 
 public class Walker : MonoBehaviour
 {
     public GameObject theCamera;
     public Transform movementBase;
     public float speed;
     public float jumpHeight;
     
     public bool askJump;
     public float distanceToGround;
     private float moveHorizontal;
     private float moveVertical;
     
     void Start()
     {
         distanceToGround = collider.bounds.extents.y;
     }
     
     void Update()
     {
         moveHorizontal = Input.GetAxis ("Horizontal");
         moveVertical = Input.GetAxis ("Vertical");
         
         if(Input.GetKey(KeyCode.Space) && isGrounded())
         {
             askJump = true;
         }
         else
         {
             askJump = false;
         }
     }
 
     void FixedUpdate()
     {
         Vector3 forwardVel = movementBase.transform.forward * speed * moveVertical;
         Vector3 horizontalVel = movementBase.transform.right * speed * moveHorizontal;
         
         rigidbody.AddForce (forwardVel + horizontalVel, ForceMode.Impulse);
         
         if(askJump == true)
         {
             var jumpVel = rigidbody.velocity;
             jumpVel.y = jumpHeight;
             
             rigidbody.velocity = jumpVel;
         }
     }
 
     public bool isGrounded()
     {
         return Physics.Raycast(transform.position, -Vector3.up, distanceToGround + 0.1f);
     }
 }

So, there it is, I don't know how to tell what I'm grounded on, so that's what I need help with.

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
Best Answer

Answer by The Stick · May 09, 2014 at 06:32 PM

There is an overload to Physics.Raycast that lets you nab more information regarding what you're hitting.

 RaycastHit hit;
 Physics.Raycast(transform.position, Vector3.down, out hit, distanceToGround + 0.1f);

This will let you utilize a `RaycastHit` to figure out what it was that raycast actually hit, like, say, a moving platform. You can get a Collider out of hit (via hit.collider), and from there extrapolate the connected GameObject.

Just a note: hit.collider will return the Collider that the raycast hit. This is the other GameObject, not your character.

Comment
Add comment · Show 7 · 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 SVGK · May 09, 2014 at 06:46 PM 0
Share

Hm, I'm getting a NullReferenceException with this technique (never knew you could get the collider this way though).

 using UnityEngine;
 using System.Collections;
 
 public class Walker : $$anonymous$$onoBehaviour
 {
     public GameObject theCamera;
     public Transform movementBase;
     public float speed;
     public float jumpHeight;
 
 public bool askJump;
 public float distanceToGround;
 private float moveHorizontal;
 private float moveVertical;
 private RaycastHit platform;
 
 void Start()
 {
     distanceToGround = collider.bounds.extents.y;
 }
 
 void Update()
 {
     moveHorizontal = Input.GetAxis ("Horizontal");
     moveVertical = Input.GetAxis ("Vertical");
     
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
     {
         askJump = true;
     }
     else
     {
         askJump = false;
     }
 }

 void FixedUpdate()
 {
     Vector3 forwardVel = movementBase.transform.forward * speed * moveVertical;
     Vector3 horizontalVel = movementBase.transform.right * speed * moveHorizontal;
     
     rigidbody.AddForce (forwardVel + horizontalVel + platform.collider.rigidbody.velocity, Force$$anonymous$$ode.Impulse);
     
     if(askJump == true && isGrounded())
     {
         var jumpVel = rigidbody.velocity;
         jumpVel.y = jumpHeight;
         
         rigidbody.velocity = jumpVel;
     }
 }

 public bool isGrounded()
 {
     return Physics.Raycast(transform.position, -Vector3.up, out platform, distanceToGround + 0.1f);
 }
 }

Apparently I haven't set some object refernce on line 42.

avatar image SVGK · May 09, 2014 at 07:08 PM 0
Share

I'm a little confused, I don't quite know what you mean by that, moving the isGrounded thing to above start doesn't do anything, and I can't initialize the isGrounded thing either.

EDIT: his reply is deleted, so this post probably looks a little out of place and strange.

avatar image mrlchristie · May 09, 2014 at 07:11 PM 0
Share

That was me, sorry. I didn't read the problem fully so miss-understood and realised what I said was nonsense so removed my comment!

avatar image SVGK · May 09, 2014 at 07:20 PM 0
Share

Okay, that's nice of you, I frequently edit posts I make as well, though I still need assistance, am I missing something to set an instance of an object to the object reference?.

avatar image SVGK · May 10, 2014 at 08:33 PM 0
Share

I've got a feeling that my problem is being caused by isGrounded being a bool, so it can only return a true or false output, nothing else.

I bet that's it, isn't it?, I'm going to have to rewrite the code by the look of it.

Show more comments

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

22 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 avatar image avatar image

Related Questions

Is this way of making a raycast correct 2 Answers

RaycastHit2D.collider is null - why? 1 Answer

How do I change the raycasthit material back? 1 Answer

Raycasting will not hit some imported meshes with mesh colliders? 2 Answers

Physics.Raycast not hitting anything 1 Answer


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