Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NathanGG · May 07, 2018 at 06:22 PM · raycastingraycasthit

Raycasting up for crouch

I made a crouch script (I put it in my moving script) and now i wish to make a raycast up, to know if the player is going to hit the roof. The crouching script just sizes down the player. Whatever, here's my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ControllingScript : MonoBehaviour {
 
     public Transform player;
     public float walkSpeed = 10f;
 
     // Use this for initialization
     void Start () 
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
     
     // Update is called once per frame
     void Update () 
     {
         float translation = Input.GetAxis ("Vertical") * walkSpeed;
         float straffe = Input.GetAxis ("Horizontal") * walkSpeed;
         translation *= Time.deltaTime;
         straffe *= Time.deltaTime;
 
             transform.Translate(straffe, 0, translation);
 
 
         if (Input.GetKeyDown (KeyCode.LeftControl))
             this.transform.localScale -= new Vector3 (0f, 0.4f);
 
         if (Input.GetKeyUp (KeyCode.LeftControl))
             this.transform.localScale += new Vector3 (0f, 0.4f);
     }
 }

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
1

Answer by Xavier78 · May 08, 2018 at 04:38 AM

There are lots of tutorials about raycasting like, https://www.youtube.com/watch?v=6agwCUaMNWI My suggestion is to also have it ignore the player object. Here is something to get you started, really just check the distance, and make sure it isn't Hitting the player also.

         Ray ray = new Ray(transform.position, Vector3.up);
 
         RaycastHit Hit;
 
         if (Physics.Raycast(ray, out Hit))
         {
             // We hit something above
             float distanceUp = Vector3.Distance(transform.position, Hit.point);
         }

Comment
Add comment · Show 5 · 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 NathanGG · May 08, 2018 at 02:42 PM 0
Share

Thanks for your help, but where do I put the code?

avatar image Happeloy NathanGG · May 08, 2018 at 04:38 PM 0
Share

It depends on when you want to do the check. If you want to do it every frame, put it in your update loop.

avatar image Xavier78 NathanGG · May 08, 2018 at 05:26 PM 0
Share

I personally would put it in the if statement of get key up. do the raycast first, and then if the distance is greater then the height change the local scale.

avatar image Kciwsolb · May 08, 2018 at 02:57 PM 0
Share

If his player is a capsule Collider and he is raycasting from the inside out, he does not need a layer mask. A raycast will not hit a Collider if the origin of the ray is inside that same Collider. If he has more than just a capsule, then he might have to worry about it, but in most cases for an fps, you don't.

avatar image NathanGG Kciwsolb · May 08, 2018 at 04:33 PM 0
Share

Thank you for noting that @$$anonymous$$ciwsolb

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

88 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 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 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 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

Can you figure out raycast origin position from RacyastHit? 2 Answers

Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers

how detect "OnRaycastOut" 1 Answer

Help with moving object towards player using Raycasting 1 Answer

raycast screenpointtoray isnt working for some reason 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