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 $$anonymous$$ · Jun 12, 2016 at 02:23 PM · meshanglestrigonometry

FOV visual representation script. Rotation causes offset

I am making a script which will visually represent the FOV of an AI. I have gotten it to work well so far. The result looks like this. alt text

Nice and dandy. Thought it would work. Until i rotated the player. I got massively skewed results and i have sat for an entire day, unable to figure this out and i am currently going bald at a never before seen rate. This is what a 90 degree rotation looks like alt text

And upon rotating the player 180 degrees the raycasts and the actual mesh are going in completely opposite directions. The offset affects the raycast rotation and not the mesh rotation. The script itself:

 using UnityEngine;
 using System.Collections;
 
 public class FOVRenderer : MonoBehaviour {
 
     //Field of view
     public float fov = 30f;
     //How many iterations to loop through? Resolution of the mesh
     public int iterations = 50;
     //Range of the view
     public float viewRange = 50f;
 
     //View mesh
     MeshFilter meshFilter;
     Mesh mesh;
     // Use this for initialization
     void Start () {
         meshFilter = gameObject.GetComponent<MeshFilter>();
         mesh = new Mesh();
         mesh.name = "FOVMesh";
         meshFilter.mesh = mesh;
     }
     
     // Update is called once per frame
     void Update () {
         //Degrees per step
         float stepAmount = fov/iterations;
         //Store vertices
         Vector3[] verts = new Vector3[iterations + 2];
         verts[0] = Vector3.zero;
         for(int i = 0; i<=iterations;i++)
         {
             Vector3 direction = DirectionFromAngle(transform.eulerAngles.y-fov/2+stepAmount*i);
             Debug.DrawRay(transform.position,direction*viewRange,Color.blue); //This works
             RaycastHit hit;
             if (Physics.Raycast(transform.position, direction, out hit, viewRange))
             {
                 verts[i + 1] = transform.InverseTransformPoint(hit.point); //This also works
             }
             else
                 verts[i + 1] = direction*viewRange; //The assigning of this somehow has a 2*y rotation
         }
         //Create triangles
         int[] tris = new int[(verts.Length - 2)*3];
         for(int i = 0; i < verts.Length-2; i++)
         {
             tris[i * 3] = 0;
             tris[i * 3 + 1] = i + 1;
             tris[i * 3 + 2] = i + 2;
         }
         mesh.Clear();
         mesh.vertices = verts;
         mesh.triangles = tris;
         mesh.RecalculateNormals();
     }
 
     Vector3 DirectionFromAngle(float angleInDegrees)
     {
         return new Vector3(Mathf.Sin(angleInDegrees*Mathf.Deg2Rad),0,Mathf.Cos(angleInDegrees*Mathf.Deg2Rad));
     }
 }
 

Any help is greatly appreciated. Thanks in advance.

Edit 1: I am stupid. The mesh is the one rotating more than it should. It rotates 2*y. The raycast is being casted correctly.

working.png (164.8 kB)
not-working.png (83.2 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
Best Answer

Answer by $$anonymous$$ · Jun 12, 2016 at 04:05 PM

I solved it! The points that was assigned by default if the raycast hit nothing had an offset of 2*y. Therefore i created a compensation for this offset like this:

verts[i + 1] = (Quaternion.Euler(0, -transform.eulerAngles.y, 0) * direction)*viewRange;

This line would rotate the point of the back 1*y, meaning the overall rotation would be 2*y - 1*y = y leaving us with the correct angle

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 JatsArts · Jun 12, 2016 at 04:12 PM 0
Share

So much math.

avatar image $$anonymous$$ JatsArts · Jun 12, 2016 at 04:17 PM 0
Share

Hahah yeah. Doesen't help that my maths grades are quite shabby at best.

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

46 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

Related Questions

How can I calculate the angle between two adjacent triangles in 3D? (for detecting concave or convex triangles) 1 Answer

How to calculate internal angles of a mesh triangle? 3 Answers

C# Code Understanding - Trigonometry + Angles - Object rotating around player and facing mouse direction 0 Answers

Need help with artillery angle of reach prediction 0 Answers

Create a Triangle Mesh. 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