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
0
Question by gtrzmbie · Jan 09, 2016 at 12:00 PM · c#2dquaternionraycasting

Raycast with Quaternion directions (C#)

I'm working with a 2D game and am trying to make a Raycast from a specific point to multiple directions, and returns RaycastHit data or where the ray itself ends if it hits nothing. I want to use these points later to create/update a mesh, and I need the shape of the mesh to conform to other colliders so it doesn't pass through them, but deforms to sort of "cast a shadow."

So far, I've managed to Debug.DrawLine basically what I'm going for, but I'm having some trouble making the code work for rays. For some reason the Unity documentation on Quaternions and rays isn't really helping that much.

Here's my code so far:

 using UnityEngine;  
 using System.Collections;  
 
 public class FlashlightController : MonoBehaviour {  
 
     private float distance = 3.0f;  
     private int segments = 64;  
 
     void Update()  
     {  
         if (Input.GetButton ("Fire1"))  
         {  
         RaycastCircle();  
         }  
     }  
 
     void RaycastCircle()  
     {  
         Vector3 mouseScreenPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);  
         Vector3 mousePos = new Vector3 (mouseScreenPos.x, mouseScreenPos.y, 0);  
         Vector3 targetPos = Vector3.zero;  
         Vector3 endPos;  
         int StartAngle = 0;  
         int finishAngle = 360;  
         int increment = (int)(finishAngle / segments);  
 
         for (int i = startAngle; i < finishAngle; i += increment)  
         {  
 
             targetPos = (Quaternion.Euler (0, 0, i) * transform.up).normalized * distance;  
             endPos = new Vector3 (mousePos.x + targetPos.x, mousePos.y + targetPos.y, 0);  
 
             Debug.DrawLine (mousePos, endPos, Color.red);  
         }  
     }  
 }  

Doing this yields results like this:circle test

I have a script to move the player with WSAD and have its rotation around the z-axis follow the mouse pointer on screen, and another script that causes the camera to follow the player as it moves, just in case those are important.

My problem is that I guess I don't really understand how to replicate that shape with Raycasting. When I try, I only get one ray which goes from the world origin to the mouse position. I believe the format I'd want to use for the ray is something like

 if (Physics.Raycast(origin, direction, out RaycastHit, float maxDistance)

but I don't know how to fill the direction part of that. The documentation just says "put a direction" and then uses transform.forward or transform.up, but I want to iterate through every direction I get from the for loop, and not from the world origin.

So here are my problems:

  1. I don't understand how to pull just a rotation/direction using a Quaternion to fill the "direction" vector - I know I sort of did it already, but that code is a slightly modified answer from another question, and to be honest I'm still fuzzy on how to use Quaternions.

  2. I can't get the rays to originate from anywhere besides the world origin.

I'm sorry this is so long, I'm just a tad frustrated at how little I feel I am gleaning from the docs.

If you made it through all of that nonsense, I really appreciate it.

successfulcircletest.jpg (456.0 kB)
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

Answer by gtrzmbie · Jan 09, 2016 at 01:37 PM

I actually just figured it out. I'm using the Physics2D system to do the Raycasting, and painting hits green and misses red. In case this is something someone else might need some day, here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class FlashlightController : MonoBehaviour {
 
     private float distance = 3.0f;
     private int segments = 64;
 
     void Update ()
     {
         if (Input.GetButton ("Fire1"))
         {
             RaycastCircle2D ();
         }
     }  
 
     void RaycastCircle2D()
     {
         Vector2 mouseScreenPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         Vector2 mousePos = new Vector2 (mouseScreenPos.x, mouseScreenPos.y);
         Vector2 targetPos = Vector2.zero;
         Vector2 endPos;
         int startAngle = 0;
         int finishAngle = 360;
         int increment = (int)(finishAngle / segments);
 
         for (int i = startAngle; i < finishAngle; i += increment)
         {
             Vector2 rayDirection2D = (Quaternion.Euler (0, 0, i) * transform.up).normalized;
             targetPos = rayDirection2D * distance;
             endPos = new Vector2 (mousePos.x + targetPos.x, mousePos.y + targetPos.y);
 
             RaycastHit2D hit2D = Physics2D.Raycast (mousePos, rayDirection2D, distance);
             if (hit2D.collider != null)
             {
                 Debug.DrawLine(mousePos, hit2D.point, Color.green);
             } else {
                 Debug.DrawLine (mousePos, endPos, Color.red);
             }
         }
     }
 }

So, figured out the Quaternion thing through yet another answer on these forums. I know it's messy code, but it helps me keep track of what I'm doing while I flounder around.

this seems to work


freakingfinally.jpg (475.1 kB)
Comment
Add comment · 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

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

70 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

Related Questions

Raycast on mouse position problem. how not to raycast to 0 1 Answer

Strafing diagonally using get.axis in Unity 5? 0 Answers

Quaternion lerp slowing down movement? 1 Answer

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

Can you make the player do a 180° / u-turn when a button is pressed? 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