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 London243 · Jun 17, 2014 at 11:14 AM · raycasttouchtargetaimdrawline

Touch and aim with a raycast line

Hello

I'm trying to fire a ray before my player shoots a ball in order to him/her aim at the object he wants to target.

I did first in my mac in order to understand it and it work with this script

     Ray ray = Camera.main.ScreenPointToRay(Input.);

     Debug.DrawRay (ray.origin, ray.direction * 100, Color.cyan);

     RaycastHit hit;



But then I did for my real game and has been no joy at all.

Here is the script I am using:

 void Update () 
 {
     if (Input.touches.Length <= 0) 
         
     {
         return;
     } 
     
     else // if there is a touch
         
     {
         //loop through all the touches on the screen 
         
         for(int i = 0 ; i < Input.touchCount; i++)
         {
             if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
                 
             {
                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
                 Plane plane = new Plane(Vector3.up, transform.position);
                 float distance = 0;
                 if (plane.Raycast(ray, out distance))
                 {
                     Vector3 pos = ray.GetPoint(distance);
                     Debug.DrawRay (ray.origin, ray.direction * 100, Color.cyan);
                     RaycastHit hit;
                     
                     if (Physics.Raycast (ray, out hit) == true) 
                     {
                         Debug.DrawRay(ray.origin, ray.direction * hit.distance, Color.red);
                     }



But nothing really happens.

can some one help and telling me where is my mistake?

Thanks

CL

Comment
Add comment · Show 1
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 Adam-Buckner ♦♦ · Jun 24, 2014 at 09:59 AM 0
Share

Fixed code tags

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Jun 17, 2014 at 11:17 AM

Debug.DrawRay() is documented as only working in the SceneView and GameView if you have gizmos. It is not intended to be a general-purpose line renderer, hence belonging to Debug class. Try:

http://docs.unity3d.com/ScriptReference/GL.html

for a general purpose line renderer.

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 London243 · Jun 17, 2014 at 03:20 PM 0
Share

I am going with what you said Graham, but I did read that Gl.line and all of the GL must be used in OnPostRednder() and not in Update() as in my case. You still advise me to try this GL or for your knowledge there is another way ?

avatar image London243 · Jun 17, 2014 at 03:30 PM 0
Share

GL class does not work on iphone therefore I can not use it in mu script . I'am trying LineRenderer Any advice on that? Thanks

avatar image
0

Answer by Adam-Buckner · Jun 24, 2014 at 09:54 AM

I see two things with this code...

With this line:

 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)

... the code within this if statement will only work for one frame at best. You are saying "If there are some touches on the screen AND if my first touch is "beginning" a touch..." so this should only work on the first frame you put the first finger on the device. Is this what you intended?

The other is what Graham Dunnett pointed out, which is Debug.DrawRay is intended for editor use only as it is a debugging tool.

Additional suggestions could be to look at the Line Renderer class: http://docs.unity3d.com/ScriptReference/LineRenderer.html ... and see if you can bend that to your will. There are also some 3rd party assets you can use like Vectrosity, but please do your research into 3rd party asssets, as I cannot endorse any of them.

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 London243 · Jun 24, 2014 at 10:11 AM 0
Share

Thank you $$anonymous$$for your answer. I Will change to stationary ins$$anonymous$$d but there is any method I can use to replace Debug.DrawRay.? I already use Liner render and I did a the other post http://answers.unity3d.com/questions/730878/ai$$anonymous$$g-to-an-object-iphone-game.html Which also led me to nowhere

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Aiming to an object (iphone game) 0 Answers

Joystick Raycast Problem? 2 Answers

How to hit two object with one raycast? 2 Answers

Creating a raycast for turret 1 Answer

throw an object by swiping on it 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