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 Skyfall106_Gaming · Jun 13, 2017 at 09:24 AM · c#unity 5rotate objectbuildingplacement

Making Building Placment rotatable

Hey! So i have a building placment wscript and I am trying to make the building rotate when you press the r key but I am having trouble.

Here is my build manager script:

[code] using UnityEngine; using System.Collections; using System.Collections.Generic;

/// /// Build manager. /// /// This Script is attached to a empty GameObject ///

[System.Serializable] public class Snapping { public bool snappingEnabled = true; public float snapRadius = 2.0f; public float snapMagin = 0.0f; public SnapSides snapSides; }

[System.Serializable] public struct SnapSides { // based on objects facing direction public bool left, right, top, bottom, front, back; }

public class BuildManager: MonoBehaviour { public int SelectedBuilding; private int LastSelectedBuilding; public GameObject[] Building;

 public List<BuildingList> buildings = new List<BuildingList>();

 public string TerrainCollisionTag;

 private bool ghostOn = false;
 private GameObject ghost;
 private BuildingCollision ghostCollision;

 private bool isFlat;
 public float maxSlopeHigh = 5f;

 public bool isBuildingEnabled { get; private set; }

 // Unused until fully implemented
 public Snapping snapping = new Snapping();

 void Start()
 {
     LastSelectedBuilding = SelectedBuilding;
     isBuildingEnabled = false;

 }

 public void ActivateBuildingmode()
 {
     isBuildingEnabled = true;
 }

 public void DeactivateBuildingmode()
 {
     isBuildingEnabled = false;
 }

 public void SelectBuilding(int id)
 {
     if (id < Building.Length && id >= 0)
     {
         LastSelectedBuilding = SelectedBuilding;
         SelectedBuilding = id;
     }
 }

 void Update()
 {
     if (!isBuildingEnabled)
     {
         if (ghost != null)
         {
             Destroy(ghost);
             ghostOn = false;
         }

         return;
     }


     Ray ray;
     RaycastHit[] hit;
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     hit = Physics.RaycastAll(ray, Mathf.Infinity);

     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;

                 break;
             }

             if (!ghostOn)
             {
                 ghost = (GameObject)Instantiate(Building[SelectedBuilding], 
                     new Vector3(hit[i].point.x,
                         hit[i].point.y, 
                         hit[i].point.z), 
                     Quaternion.identity);

                 ghost.name = ghost.name.Replace("(Clone)", "(Ghost)");
                 ghost.layer = 2; //ignore raycast layer

                 ghostCollision = ghost.AddComponent<BuildingCollision>();

                 ghostOn = true;    
             }

             if (ghost != null)
             {
                 if (Input.GetMouseButtonDown(0) && !ghostCollision.Collided() && 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].GetComponent<Collider>().transform.localScale.y / 2,
                             hit[i].point.z),
                         Quaternion.identity);

                     string s = bl.buildingGameObject.name.Replace("(Clone)", "");
                     bl.buildingGameObject.name = s;
                     bl.buildingName = s;
                     bl.buildingGameObject.AddComponent<BuildingCollision>();

                     buildings.Add(bl);

                     ghostOn = false;

                     DeactivateBuildingmode();

                     break;
                 }

                 Vector3 ghostTargetPos = new Vector3(
                     hit[i].point.x,
                     hit[i].point.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
                     hit[i].point.z);

                 ghost.transform.position = ghostTargetPos;

                 isFlat = GroundFlat();

                 if (ghostCollision.Collided() || !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 (!ghostCollision.Collided() && isFlat)
                 {
                     ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial);
                     ghost.GetComponent<Renderer>().material.color = new Color(
                         0f,
                         1f, 
                         0f, 
                         0.6f);
                 }

             }
         }    
     }
 }

 private bool GroundFlat()
 {
     RaycastHit[] buildingSlopeHitULAll;
     RaycastHit[] buildingSlopeHitURAll;
     RaycastHit[] buildingSlopeHitDLAll;
     RaycastHit[] buildingSlopeHitDRAll;
     RaycastHit[] buildingSlopeHitMAll;

     buildingSlopeHitULAll = Physics.RaycastAll(new Vector3(
         ghost.GetComponent<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
         ghost.GetComponent<Collider>().transform.position.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
         ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
         Vector3.down, Mathf.Infinity);

     buildingSlopeHitURAll = Physics.RaycastAll(new Vector3(
         ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
         ghost.GetComponent<Collider>().transform.position.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
         ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
         Vector3.down, Mathf.Infinity);

     buildingSlopeHitDLAll = Physics.RaycastAll(new Vector3(
         ghost.GetComponent<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
         ghost.GetComponent<Collider>().transform.position.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
         ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
         Vector3.down, Mathf.Infinity);

     buildingSlopeHitDRAll = Physics.RaycastAll(new Vector3(
         ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
         ghost.GetComponent<Collider>().transform.position.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
         ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
         Vector3.down, Mathf.Infinity);

     buildingSlopeHitMAll = Physics.RaycastAll(new Vector3(
         ghost.GetComponent<Collider>().transform.position.x,
         ghost.GetComponent<Collider>().transform.position.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
         ghost.GetComponent<Collider>().transform.position.z),
         Vector3.down, Mathf.Infinity);

     RaycastHit hitUL = new RaycastHit(), hitUR = new RaycastHit(), hitDL = new RaycastHit(), hitDR = new RaycastHit(), hitM = new RaycastHit();

     foreach (RaycastHit hit in buildingSlopeHitULAll)
     {
         if (hit.collider.tag == TerrainCollisionTag)
         {
             hitUL = hit;
             break;
         }
     }
     foreach (RaycastHit hit in buildingSlopeHitURAll)
     {
         if (hit.collider.tag == TerrainCollisionTag)
         {
             hitUR = hit;
             break;
         }
     }
     foreach (RaycastHit hit in buildingSlopeHitDLAll)
     {
         if (hit.collider.tag == TerrainCollisionTag)
         {
             hitDL = hit;
             break;
         }
     }
     foreach (RaycastHit hit in buildingSlopeHitDRAll)
     {
         if (hit.collider.tag == TerrainCollisionTag)
         {
             hitDR = hit;
             break;
         }
     }
     foreach (RaycastHit hit in buildingSlopeHitMAll)
     {
         if (hit.collider.tag == TerrainCollisionTag)
         {
             hitM = hit;
             break;
         }
     }

     if ((buildingSlopeHitULAll.Length > 0 ? hitUL.collider != null : false) &&
         (buildingSlopeHitURAll.Length > 0 ? hitUR.collider != null : false) &&
         (buildingSlopeHitDLAll.Length > 0 ? hitDL.collider != null : false) &&
         (buildingSlopeHitDRAll.Length > 0 ? hitDR.collider != null : false) &&
         (buildingSlopeHitMAll.Length > 0 ? hitM.collider != null : false))
     {

         if (HitDistanceSmallerEqual(hitUL, maxSlopeHigh) &&
             HitDistanceSmallerEqual(hitUR, maxSlopeHigh) &&
             HitDistanceSmallerEqual(hitDL, maxSlopeHigh) &&
             HitDistanceSmallerEqual(hitDR, maxSlopeHigh) &&
             HitDistanceSmallerEqual(hitM, maxSlopeHigh))
         {
             return true;
         }
         else
             return false;
     }
     else
         return false;
 }

 private bool ContainsTag(RaycastHit[] hitArr, string tag)
 {
     foreach (RaycastHit h in hitArr)
         if (h.collider.tag == tag)
             return true;

     return false;
 }

 private bool ContainsTag(RaycastHit[] hitArr, string tag, out RaycastHit correctHit)
 {
     foreach (RaycastHit h in hitArr)
         if (h.collider.tag == tag)
         {
             correctHit = h;
             return true;
         }

     correctHit = new RaycastHit();
     return false;
 }

 private bool HitDistanceSmallerEqual(RaycastHit hit, float val)
 {
     if (hit.distance - (ghost.transform.localScale.y) <= val)
         return true;

     return false;
 }

 static void ClearConsole()
 {
     // This simply does "LogEntries.Clear()" the long way:
     var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
     var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
     clearMethod.Invoke(null, null);
 }

}

[/code]

Comment
Add comment · Show 2
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 NoseKills · Jun 13, 2017 at 04:00 PM 0
Share

Over 300 lines of code and I can't even spot a Input.Get$$anonymous$$eyDown() or such in there (pressing the R)... You should atleast mention what you are trying to rotate and where in the code if you want to have any chance of someone getting to know the code well enough to give any kind of answer.

avatar image Skyfall106_Gaming NoseKills · Jun 17, 2017 at 09:33 AM 0
Share

Sorry for the late reply. Would you like my GUIScript?

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

350 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 make drawable Paths 0 Answers

Making Building Placement Buildings rotateable 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Objects Stuff 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