Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DADDADDAD · Mar 09 at 09:05 AM · raycastmeshraycasthit2d

Raycast seems to stop working as the y cor of the player (origin of the raycast) changes by more than a certain amount

It is attatched to a raycast object that has the player object as a serialized field and a layermask with the wall and ground layers. The layers are set properly for all the objects.

for some reason the ray cast just stops working as the player goes up or down by more than some amount

link to video of it happening https://youtu.be/Evzr-Fdk2TQ

the purpose is to use this raycast/mesh to only render the area the player is currently in

this is my raycast code. (the 370 fov and raycount is to stop flickering, the problem persists even with those numbers set lower)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FieldOfView : MonoBehaviour
 {
     [SerializeField] PlayerMove player;
     [SerializeField] LayerMask layermask;
     private Mesh mesh;
     private void Start()
     {
         mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
     }
 
     private void Update()
     {
         float fov = 370f;
         Vector3 origin = player.transform.position;
         int raycount = 370;
         float angle = 0f;
         float angleIncrease = fov / raycount;
         float viewDistance = 20f;
 
         Vector3[] verts = new Vector3[raycount +2];//setting up verts
         Vector2[] uv = new Vector2[verts.Length];//setting up uv
         int[] tris = new int[raycount*3];//setting up tris
 
         verts[0] = origin;
 
         int vertIndex = 1;
         int triIndex = 0;
         //cycle through all rays
         for (int i=0; i<=raycount; i++)
         {
             Vector3 vertex;
             //vertex = origin + GetVectorFromAngle(angle) * viewDistance;
             RaycastHit2D raycasthit2D = Physics2D.Raycast(origin, GetVectorFromAngle(angle), viewDistance, layermask);
             if (raycasthit2D.collider == null)
             {
                 //did NOT hit object
                 vertex = origin + GetVectorFromAngle(angle) * viewDistance;//locate rays
             }
             else
             {
                 //did hit object
                 vertex = raycasthit2D.point;
             }
             verts[vertIndex] = vertex;
 
             //generate tris
             if (i>0)
             {
                 tris[triIndex+0] = 0;
                 tris[triIndex + 1] = vertIndex - 1;
                 tris[triIndex + 2] = vertIndex;
 
                 triIndex += 3;
             }
             vertIndex++;
             //move to next angle
             angle -= angleIncrease;
         }
 
         //upload all values to mesh
         mesh.vertices = verts;
         mesh.uv = uv;
         mesh.triangles = tris;
     }
 
     public Vector3 GetVectorFromAngle(float angle)
     {
         float angleRad = angle * (Mathf.PI / 180f);//0.01745f = (Mathf.PI / 180f)
         return new Vector3(Mathf.Cos(angleRad), Mathf.Sin(angleRad));
     }
 }


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

0 Replies

· Add your reply
  • Sort: 

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

239 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

Related Questions

Inconsistent raycast2D reflecting 0 Answers

Need help in Raycast2D!! 1 Answer

Rigidbody blocking raycasts 2D 0 Answers

Raycast2D 360 Rotation 0 Answers

Tag doesn't work when gameObject is a child of the Main camera? 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