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 /
avatar image
0
Question by AbdallahBenZkunim · Aug 19, 2021 at 11:26 PM · c#colliderterrainspawnplane

How can I use raycasthit and collider raycast to hit only if the mouse position is over a specific object ?

 private void Update()
     {
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (terrainCollider.Raycast(ray, out hit, Mathf.Infinity))
             {
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 cube.transform.position = hit.point;
                 cube.transform.localScale = new Vector3(50, 50, 50);
             }
         }
     }

This is working fine when I click with the mouse on the terrain it will spawn a cube on that position I clicked with the mouse.

But now I added a plane a small size plane above the terrain the plane is about 0.5 higher from the terrain.

I want that I click with the mouse it will spawn the cube/s on the terrain but only if the mouse is inside the plane area. Not anywhere on the terrain like it is now but only on the plane area.

I mean that only when I click with the mouse somewhere on the plane area than spawn cube on the terrain.

On the top of the script I reference to my plane :

 public CustomPlane plane;

And this is the CustomPlane script :

 using UnityEngine;
 
 [ExecuteAlways]
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class CustomPlane : MonoBehaviour {
 
     public float width = 1;
     public float length = 1;
 
     private float oldWidth;
     private float oldHeight;
 
     public void Start()
     {
         oldWidth = width;
         oldHeight = length;
 
         Create();
     }
 
     private void Update()
     {
         if(oldWidth != width || oldHeight != length)
         {
             Create();
 
             oldWidth = width;
             oldHeight = length;
         }
     }
 
     private void Create()
     {
         MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
 
         Mesh mesh = new Mesh();
 
         Vector3[] vertices = new Vector3[4]
         {
             new Vector3(0, 0, 0),
             new Vector3(width, 0, 0),
             new Vector3(0, length, 0),
             new Vector3(width, length, 0)
         };
         mesh.vertices = vertices;
 
         int[] tris = new int[6]
         {
             // lower left triangle
             0, 2, 1,
             // upper right triangle
             2, 3, 1
         };
         mesh.triangles = tris;
 
         Vector3[] normals = new Vector3[4]
         {
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward
         };
         mesh.normals = normals;
 
         Vector2[] uv = new Vector2[4]
         {
             new Vector2(0, 0),
             new Vector2(1, 0),
             new Vector2(0, 1),
             new Vector2(1, 1)
         };
         mesh.uv = uv;
 
         meshFilter.mesh = mesh;
     }
 }


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 joan_stark · Aug 24, 2021 at 07:27 PM

An easy way of doing this is adding a collider to your plane, and put it into an special layer. Then you can first cast a raycast to that layer, if it impacts, then you cast another raycast ignoring that specific layer. If it doesn't impact, you don't do nothing since you are not inside the plane.

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

121 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

Related Questions

Distribute terrain in zones 3 Answers

Gameobject collision with Terrain C# 4 Answers

Is it possible to convert a Collider into a Spawner? 0 Answers

Which setup should I use for spawning? 0 Answers

Multiple Cars not working 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