Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 unity_6BC4D835E42552E43E9D · Apr 22 at 03:38 PM · transformmeshprefabspawn

Problem with spawn custom mesh on prefab terrain

Hello Guys,

I'm learning Unity and C#. I have created own mesh in shape of pyramid and made prefab from it. I want to spawn those meshes on the ground surface, however have found it difficult as it seams that transform.position is somehow broken for my mesh or I'm doing something wrong.

When tried to place meshes without using transform.position meshes spawned Perfectly on ground, however on the Inspector coordinates had value 0 (which couldn't be position of my mesh). When I try to place my pyramids basing on raycast hist, objects are spawn high above the ground (picture) instead on it. I've also made simple prefab from 3D Cube and then wanted to see if same problem occur. However Cubes spawns perfectly (second picture).

So could give me some solutions what am I doing wrong? I would appreciate any help as I'm stuck for almost week.

Below I attach also codes for generating pyramid and spawn objects:

BOID:

 using System;
 using UnityEngine;
 using UnityEngine.AI;
 using Random = UnityEngine.Random;
 
 public class Boid : MonoBehaviour
 {
 
     private Vector3 _position;
     private Color _selectedColor = Color.white;
     private NavMeshAgent _agent;
     
 
     public void Start()
     {
         _position = gameObject.transform.position;
         MeshFilter meshFilter = GetComponent<MeshFilter>();
         if (meshFilter == null)
         {
             Debug.LogError("MeshFilter not found!");
             return;
         }
         MeshCollider meshCollider = gameObject.GetComponent<MeshCollider>();
         if (meshCollider == null)
         {
             Debug.LogError("MeshCollider not found!");
             return;
         }
 
         MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
         if (meshRenderer == null)
         {
             Debug.LogError("MeshRenderer not found!");
             return;
         }
         
         float X = _position.x;
         float Y = _position.y;
         float Z = _position.z;
         
         Vector3 p0 = new Vector3(X, Y + 1, Z);
         Vector3 p1 = new Vector3(X + 1,Y + 1, Z);
         Vector3 p2 = new Vector3(X + 1,Y, Z);
         Vector3 p3 = new Vector3(X, Y, Z);
         Vector3 p4 = new Vector3((X + X +1) / 2, (Y + Y + 1) / 2, Z + 1);
        
         Mesh mesh = new Mesh();
         
         mesh.vertices = new Vector3[]{
             p1,p2,p0,
             p2,p3,p0,
             p4,p1,p0,
             p4,p2,p1,
             p4,p3,p2,
             p4,p0,p3
         };
         mesh.triangles = new int[]{
             0,1,2,
             3,4,5,
             6,7,8,
             9,10,11,
             12,13,14,
             15,16,17
         };
         Vector2 uv0  = new Vector2(0.25f,0.25f);
         Vector2 uv3  = new Vector2(0.25f,0.75f);
         Vector2 uv1 = new Vector2(0.75f, 0.25f);
         Vector2 uv2 = new Vector2(0.75f, 0.75f);
         Vector2 uv4a = new Vector2(0,0.5f);
         Vector2 uv4b = new Vector2(0.5f, 1);
         Vector2 uv4c = new Vector2(1, 0.5f);
         Vector2 uv4d = new Vector2(0.5f, 0);
 
         mesh.uv = new Vector2[]{
             uv0,uv1,uv3,
             uv1,uv2,uv3,
             uv0,uv1,uv4d,
             uv1,uv2,uv4c,
             uv2,uv3,uv4b,
             uv3,uv0, uv4a
         };
         meshRenderer.material = Resources.Load("BasicMaterial") as  Material;
         meshRenderer.material.color = _selectedColor;
         // meshRenderer.materials = materials.ToArray();
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.Optimize();
         
         meshCollider.sharedMesh = mesh;
         meshFilter.sharedMesh = mesh;
     }
 
     public void SetSelectedVisible(bool visible)
     {
         if (visible)
         {
             _selectedColor = Color.red;
         }
         else
         {
             _selectedColor = Color.white;
         }
         gameObject.GetComponent<MeshRenderer>().material.color = _selectedColor;
     }
 
     public void MoveTo(Vector3 position)
     {
         _agent.SetDestination(position);
         
     }
 }
 

SPAWING:

 using System;
 using TMPro;
 using UnityEngine;
 using UnityEngine.AI;
 using Button = UnityEngine.UI.Button;
 
 public class SpawnObjects : MonoBehaviour
 {
     public Button buttonClick;
     public TMP_InputField spawnNumber;
 
     private readonly Vector3 _initCameraPosition = new (0.0f, 100.0f, 0.0f);
     private GameObject _spawnTerrain;
 
     private float maxHeight = 100.0f;
     private RaycastHit _raycastHit;
     private int _currentObjectNumber;
     private int _columnNumber;
     private void Start()
     {
         _spawnTerrain = GameObject.FindGameObjectWithTag("Respawn");
         Debug.Log(_spawnTerrain);
         
         buttonClick.onClick.AddListener(Spawn);
     }
     public void Spawn()
     {
         int numberOfSpawnObject = int.Parse(spawnNumber.text);
         int objectsInRowNumber = getNumberOfObjectInRow(numberOfSpawnObject);
         for (int i = 0; i < numberOfSpawnObject; i++)
         {
             if (_currentObjectNumber == objectsInRowNumber)
             {
                 _currentObjectNumber = 0;
                 _columnNumber += 1;
             }
             
             float x = _initCameraPosition.x + 2 * _currentObjectNumber;
             float y = _initCameraPosition.y;
             float z = _initCameraPosition.z  + 2 * _columnNumber;
 
             Vector3 init = new Vector3(x, y, z);
             Vector3 spawnPosition = Vector3.zero;
             
             
             if (Physics.Raycast(init, Vector3.down, out _raycastHit))
             {
                 Debug.Log("HIT:" + _raycastHit.point);
                 spawnPosition = _raycastHit.point;
                 Debug.DrawRay(spawnPosition, Vector3.down, Color.blue, 10.0f);
             }
 
             GameObject boid = Resources.Load("Block") as GameObject;
             boid.transform.position = spawnPosition;
             Instantiate(boid);
             _currentObjectNumber += 1;
         }
     }
 
     private int getNumberOfObjectInRow(int objectNumber)
     {
         return (int) Math.Sqrt(objectNumber);
     }
 }
 


[1]: /storage/temp/195371-boids.png

blocks.png (108.7 kB)
boids.png (103.3 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

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

212 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

Related Questions

How to Spawn a Prefab over an Array of Transforms, One Prefab at a Time? 1 Answer

Prefab can't be transformed in editor after import from a package. 1 Answer

Setting Position of Spawned Prefab 2 Answers

I can modify the GameObject transform using the inspector, how can I modify the mesh as well? 0 Answers

Spawning unique prefabs at different transform posititions 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