Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Coleraone · Jun 16, 2015 at 05:44 AM · rotationinstantiate

Instantiate question

Hi, i got this asset from the asset store for building placement. There's this ghost building before the final building places. And when the final building places i would like it to also instantiate with the ghost buildings z rotation because i have added a script so the ghost can be rotated before the final object is instantiated.

just to help clarify the question ill give an example say this line is my ghost object | When i rotate the ghost like this \ the final object still instantiates like | like it was originaly Here's the Code-

 public class BuildManager: MonoBehaviour 
 {
     public int SelectedBuilding;
     private int LastSelectedBuilding;
     public GameObject[] Building;
     
     public List<GameObject> collided = new List<GameObject>();
     public List<BuildingList> buildings = new List<BuildingList>();
     
     public string TerrainCollisionTag;
     
     private GameObject ghost;
 
     private bool ghostOn = false;
 
     private bool isFlat;
     public float maxSlopeHigh = 5f;
     
     void Start()
     {
         LastSelectedBuilding = SelectedBuilding;
     }
     
     void Update()
     {
         Ray ray;
         RaycastHit[] hit;
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         hit = Physics.RaycastAll(ray, Mathf.Infinity);
         
 
 if(GUIScript.TimeToPlace==true){
         
         for (int i = 0; i < hit.Length; i++)
         {
             if (hit[i].collider.tag == TerrainCollisionTag)
             {
                 if (SelectedBuilding != LastSelectedBuilding && ghost != null)
                 {
                     Destroy(ghost);
                     ghostOn = false;
                     LastSelectedBuilding = SelectedBuilding;
                     collided.Clear();
                     break;
                 }
                 
                 if (!ghostOn)
                 {
                     ghost = (GameObject)Instantiate(Building[SelectedBuilding], 
                     new Vector3(hit[i].point.x,
                         hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2), 
                         hit[i].point.z), 
                         Quaternion.identity);
                         
                     ghostOn = true;    
                 }
             
         
 
                 if (Input.GetMouseButtonDown(0) && collided.Count == 0 && isFlat )
                 {
                     BuildingList bl = new BuildingList();
                     
                     DestroyImmediate(ghost);
                     
                     bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding], 
                     new Vector3(hit[i].point.x, 
                         hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2), 
                         hit[i].point.z), 
                     Quaternion.identity);
                     
                     string s = bl.buildingGameObject.name.Replace("(Clone)", "");
                     bl.buildingGameObject.name = s;
                     bl.buildingName = s;
                     
                     buildings.Add(bl);
                 
                     ghostOn = false;
                         GUIScript.TimeToPlace=false;
 
                     collided.Clear();
                     break;
                         
 
                 }
                 
                 if (ghost != null)
                 {
                     ghost.transform.position = new Vector3(
                         hit[i].point.x, 
                         hit[i].point.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2, 
                         hit[i].point.z);
                             
                     isFlat = GroundFlat(ghost);
                         
                     if (collided.Count != 0 || !isFlat)
                     {
                         ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial);
                         ghost.GetComponent<Renderer>().material.color = new Color(
                             1f,
                             0f, 
                             0f, 
                             0.6f);
                     }            
                     else if (collided.Count == 0 && isFlat)
                     {
                         ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial);
                         //ghost.GetComponent<Renderer>().material.color = new Color(
                         //    0f,
                         //    1f, 
                         //    0f, 
                         //    0.6f);
                     }
             
                 }
             }    
         }
     }
     }
 
     bool GroundFlat(GameObject Ghost)
     {
         RaycastHit[] buildingSlopeHitUL;
         RaycastHit[] buildingSlopeHitUR;
         RaycastHit[] buildingSlopeHitDL;
         RaycastHit[] buildingSlopeHitDR;
         RaycastHit[] buildingSlopeHitM;
         
         buildingSlopeHitUL = Physics.RaycastAll(new Vector3(
             ghost.GetComponent<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
             ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
             ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
             Vector3.down, Mathf.Infinity);
         
         buildingSlopeHitUR = Physics.RaycastAll(new Vector3(
             ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
             ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
             ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
             Vector3.down, Mathf.Infinity);
         
         buildingSlopeHitDL = Physics.RaycastAll(new Vector3(
             ghost.GetComponent<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
             ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
             ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
             Vector3.down, Mathf.Infinity);
         
         buildingSlopeHitDR = Physics.RaycastAll(new Vector3(
             ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
             ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
             ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
             Vector3.down, Mathf.Infinity);
         
         buildingSlopeHitM = Physics.RaycastAll(new Vector3(
             ghost.GetComponent<Collider>().transform.position.x,
             ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
             ghost.GetComponent<Collider>().transform.position.z),
             Vector3.down, Mathf.Infinity);
         
         for (int i = 0; i < buildingSlopeHitM.Length; i++)
         {
             if ((buildingSlopeHitUL[i].collider.tag == TerrainCollisionTag) &&
                 (buildingSlopeHitUR[i].collider.tag == TerrainCollisionTag) &&
                 (buildingSlopeHitDL[i].collider.tag == TerrainCollisionTag) &&
                 (buildingSlopeHitDR[i].collider.tag == TerrainCollisionTag) &&
                 (buildingSlopeHitM[i].collider.tag == TerrainCollisionTag))
             {
                 if ((buildingSlopeHitUL[i].distance <= maxSlopeHigh) &&
                     (buildingSlopeHitUR[i].distance <= maxSlopeHigh) &&
                     (buildingSlopeHitDL[i].distance <= maxSlopeHigh) &&
                     (buildingSlopeHitDR[i].distance <= maxSlopeHigh) &&
                     (buildingSlopeHitM[i].distance <= maxSlopeHigh))
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             else
             {
                 return false;
             }
             
 
         }
         return false;
         
     }
     
 
     
 }
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
1
Best Answer

Answer by Yggdraseal · Jun 16, 2015 at 01:11 PM

The problem is in this line :

 bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding], 
                      new Vector3(hit[i].point.x, 
                          hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2), 
                          hit[i].point.z), 
                      Quaternion.identity);

Quaternion.identity means the objets is instantiated with it's "original" orientation.

Try setting it to your ghost building's rotation.

 Quaternion myNewRot = ghost.transform.rotation;

and then intantiating :

 bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding], 
                      new Vector3(hit[i].point.x, 
                          hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2), 
                          hit[i].point.z), 
                      myNewRot);




Comment
Add comment · Show 3 · 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 Coleraone · Jun 16, 2015 at 02:23 PM 0
Share

Assets/Placement $$anonymous$$it/Placement $$anonymous$$it Files/Scripts/Build$$anonymous$$anager.cs(73,69): error CS1061: Type UnityEngine.GameObject' does not contain a definition for rotation' and no extension method rotation' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

i get this error when i do that?

avatar image Coleraone · Jun 16, 2015 at 02:27 PM 0
Share

found out you just have to put transform before rotation

avatar image Yggdraseal · Jun 16, 2015 at 04:56 PM 0
Share

Silly me ! I guess I answered too hastily ! Edited.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is my projectile firing "off" after rotation 1 Answer

How to rotate an Instantiated object? 1 Answer

How to create GameObject (from prefab) at location of BoxCollider2D with rotation? 0 Answers

Shooting with variable vector3 0 Answers

Changing build rotation 2 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