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 /
  • Help Room /
avatar image
1
Question by Nooke · Dec 29, 2016 at 05:56 PM · c#positionscripting beginnerraycasting

Raycast positioning problem

Hi, I'm trying to debug.log :"Position available" when the player looks at an object that is 5 or less units away. I'm having an issue with my raycast's origin and direction though: they don't start anywhere near my player gameobject (at least not by the looks of my debug.drawray) and "position available" is never output by the console. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Phase1 : MonoBehaviour
 {
 
     private float DistanceToPosition = 5;
     public void Positioning ()
     {
         RaycastHit hit;
         Ray PositioningRay = new Ray(transform.position, transform.forward);
         if (Input.GetButtonDown("Fire1"))
             {
                 Debug.DrawRay(gameObject.transform.position, gameObject.transform.forward * DistanceToPosition);
                 if (Physics.Raycast(PositioningRay, out hit, DistanceToPosition))
                 {
 
                     if (hit.distance < DistanceToPosition)
                     {
                         Debug.Log("PositionAvailable");
                     }
                 
                 }
             }
     }
 }
 

By the way the function is called from another script. I'd greatly appreciate any help.

Comment
Add comment · Show 2
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 UnityCoach · Dec 29, 2016 at 06:19 PM 0
Share

If I'm not mistaken, Input.GetButtonDown is only available within Update and FixedUpdate.

avatar image Nooke UnityCoach · Dec 29, 2016 at 06:37 PM 0
Share

Debug.DrawRay does appear, just not at the correct position, so the if statement has got to be correct I think (correct me if I'm missing something, I'm a beginner).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ahungrybear · Dec 29, 2016 at 08:53 PM

Hmmm. The code itself look fine... (I'm assuming the script is attached to the player)

Where is the ray starting relative to your character?

Also you don't need gameObject.transform.position, just transform.position is fine

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 Nooke · Dec 29, 2016 at 09:37 PM 0
Share

Yeah, the script is attached to the player. The ray's position doesn't change relative to my character's position, the transform position is : (4.1, 1.0, -2.2) where as the player spawns at (-6.88, 0.1, -6.56). There's also no other gameobject or script referencing that position which completely confuses me.

avatar image ahungrybear Nooke · Dec 29, 2016 at 09:56 PM 0
Share

Hmmm. Yeah it seems like you've got a good problem on your hands. When is this function called btw? Is it continuous? Also could you share a screenshot of your scene? I can't seem to find anything wrong with the script on my end.

avatar image Nooke ahungrybear · Dec 29, 2016 at 10:13 PM 0
Share

the function is called from the following script which is on the Game$$anonymous$$anager object you can see in the pic below:

 using UnityEngine;
 using System.Collections;
 
 public class Phase : $$anonymous$$onoBehaviour
 {
 
     public int TimeLeft = 60;
     public GameObject ScriptAttachedTo;
 
     void Start()
     {
         InvokeRepeating("PreparationPhase",0,1);
     }
     void Update()
     {
         if (TimeLeft > 0)
         {
             ScriptAttachedTo.GetComponent<Phase1>().Positioning();
         }
     }
     void PreparationPhase()
     {
         Debug.Log(TimeLeft);
         TimeLeft -= 1;
         if(TimeLeft == 0)
         {
             Debug.Log("End of Preparation Phase");
             CancelInvoke();
         }
     }
 
 }
 

so yeah, it is continuous. Here is a screenshot of my scene (networked so the player prefab isn't spawned in):

alt text

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

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

Raycasting a "tractor beam" in a football game 0 Answers

Shoot towards mouse except mouse y 1 Answer

error CS1061: Type `UnityEngine.LineRenderer' does not contain a definition for `numPositions' and no extension method `numPositions' of type `UnityEngine.LineRenderer' could be found 1 Answer

Changing a variable in another script on Raycast hit - C# 1 Answer

Tetxure swap using HTC controllers 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