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 scribblenaughts · Jan 07, 2020 at 10:11 PM · crosshairphysics.raycastdistance checkhook

Crosshair Distance Indicator

I have this grappling hook that can travel a certain max distance towards terrain and will return if it doesn't hit anything.

I want the crosshair to indicate a red cross if the distance of the terrain that the player is looking at is too far to grapple to, but remain the same if the player is close enough to grapple to the terrain.

A suggestion was to cast a Physics Ray from the camera and see the distance from the ray to an object, but I don't know how I would achieve this, nor if this would actually solve the issue. I thought maybe I could use the float "distanceToHook" as depicted in the first script. Can someone point me in the right direction? Thanks :)

SCRIPTS:

This is the first grappling hook script.

 public class GrapplingHook : MonoBehaviour {

 public GameObject player;

 public GameObject hook;
 public GameObject hookHolder;

 public float hookTravelSpeed;
 public float playerTravelSpeed;

 public static bool fired;
 public bool hooked;
 public GameObject hookedObject;

 public float maxDistance;
 private float currentDistance;

 void Update()
 {
     if (Input.GetMouseButtonDown(0) && fired == false)
     {
         fired = true;
     }

     if (fired == true && hooked == false)
     {
         hook.transform.Translate(Vector3.forward * Time.deltaTime * hookTravelSpeed);
         currentDistance = Vector3.Distance(transform.position, hook.transform.position);

         if(currentDistance >= maxDistance)
         {
             ReturnHook();
         }
     }

     if (hooked == true)
     {
         hook.transform.parent = hookedObject.transform;
         transform.position = Vector3.MoveTowards(transform.position, hook.transform.position, Time.deltaTime * playerTravelSpeed);
         float distanceToHook = Vector3.Distance(transform.position, hook.transform.position);

^^^^^^^^^^here is the variable i thought i could use.^^^^^^^^^

         player.GetComponent<PlayerMovement>().gravity = 0f;

         if (Input.GetMouseButtonDown(0)  ||  distanceToHook < 2)
         {
             ReturnHook();
         }
     }
     else
     {
         hook.transform.parent = hookHolder.transform;
         player.GetComponent<PlayerMovement>().gravity = -35f;
     }

 }

 void ReturnHook()
 {
     hook.transform.rotation = hookHolder.transform.rotation;
     hook.transform.position = hookHolder.transform.position;
     fired = false;
     hooked = false;
 }


This is the other script that detects if the terrain can be hooked or not.

     public class HookDetector : MonoBehaviour {
 public GameObject player;

 void OnTriggerEnter(Collider other)
 {
       if(other.tag == "Hookable")
       {
         player.GetComponent<GrapplingHook>().hooked = true;
         player.GetComponent<GrapplingHook>().hookedObject = other.gameObject;
     }
 }
 }
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 ThomasOr · Jan 07, 2020 at 10:46 PM

You could run the check like this:

 // Pre-distance checks such as mouseDown and notHooked
 
 RaycastHit hit;
 Ray ray = Camera.MainCamera.ScreenPointToRay(0.5, 0.5); // You can use Input.mousePosition as an alternative to the middle of the screen (0.5, 0.5)
 if (Physics.Raycast(ray, out hit)) {
   if (hit.distance <= maxHookDistance) {
      progressWithHook(hit); // A method that will deal with the actual hook functionality
   } else {
      badHook(1); // A method that creates the red cross etc.
 } else {
   badHook(0); // Possible fail states such as 0 being noObjectSelected and 1 being maxDistanceExceeded etc.
 }

That code creates a ray from the centre currently outputting camera (unless you replaced 0.5, 0.5) which will tell you (through a RaycastHit object instantiated as hit) if it hit an object (this is what Physics.Raycast returns), the GameObject which it hit and a distance calculation (of C# type float). Additionally, you could use the hit GameObject to check for a tag or scripts.


Feel free to comment on this or DM me (no idea if unity answers has DM ngl, also hope that unity doesn't rep limit comments (fairly sure it doesn't)) if you've (being anyone reading this) got any questions.

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 scribblenaughts · Jan 08, 2020 at 09:49 PM 0
Share

Apparently the ScreenPointToRay(0.5,0.5) cannot convert the double to a float. Should I use mouse position ins$$anonymous$$d? Also, the hook is slightly off centred to make it seem like it is held in the right hand. It doesn't go directly towards the centre of the screen. How can I propel the hook towards the centre of the screen ins$$anonymous$$d of just forwards?

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

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

Crosshair disappears? 2 Answers

first person crosshair 3 Answers

FPS Tutorial problem 1 Answer

Moving Object At MousePosition.X 1 Answer

Need to lock the mouse but not in middle - OCULUS RIFT 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