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 /
This question was closed Jul 04, 2020 at 01:13 AM by Sevotastic for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Sevotastic · Jan 06, 2020 at 07:33 AM · bugglitchannoying

Weird Placement Glitch

Hi, I have got this glitch that has been bugging me for days and I really haven't found the answer to fixing it . The bug goes as follows, I press play in unity then hit a hotkey to enable placement of trees on a plane but them it sort of phases in and out and glitches between placing and destroying, this makes it nearly impossible to place the asset. (BTW this is only to create a game mechanic so I have a base to build on)

The script: using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic;

 public class GroundPlacementController : MonoBehaviour
 {
     public List<GameObject> trees = new List<GameObject>();
 
     private GameObject placeableObjectPrefab;
 
     private bool isToggled = false;
 
     public Button firTree;
     public Button oakTree;
     public Button palmTree;
     public Button poplarTree;
 
     private int i = 0;
 
     [SerializeField]
     private KeyCode newObjectHotkey = KeyCode.A;
 
     [SerializeField]
     private KeyCode cycleObjectHotKey = KeyCode.Tab;
 
     private GameObject currentPlaceableObject;
 
     private float mouseWheelRotation;
 
     private void Start()
     {
         Button fTB = firTree.GetComponent<Button>();
         fTB.onClick.AddListener(TreeZero);
         Button oTB = oakTree.GetComponent<Button>();
         oTB.onClick.AddListener(TreeOne);
         Button pTB = palmTree.GetComponent<Button>();
         pTB.onClick.AddListener(TreeTwo);
         Button ppTB = poplarTree.GetComponent<Button>();
         ppTB.onClick.AddListener(TreeThree);
     }
 
     private void Update()
     {
         placeableObjectPrefab = trees[i];
 
 
 
         if (Input.GetKeyDown(newObjectHotkey))
         {
             isToggled = true;
         }
 
         HandleNewObjectBool();
 
         if (currentPlaceableObject != null)
         {
             MoveCurrentObjectToMouse();
             RotateFromMouseWheel();
             ReleaseIfClicked();
         }
 
         if (Input.GetKeyDown(cycleObjectHotKey))
         {
             i++;
             print(placeableObjectPrefab);
             if (i == 4)
             {
                 i = i * 0;
             }
         }
 
 
     }
 
     private void HandleNewObjectBool()
     {
         if (isToggled == true)
         {
             if (currentPlaceableObject != null)
             {
                 Destroy(currentPlaceableObject);
             }
             else
             {
                 currentPlaceableObject = Instantiate(placeableObjectPrefab);
             }
         }
 
 
     }
     private void PlaceNewObject()
     {
 
     }
 
     private void MoveCurrentObjectToMouse()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             currentPlaceableObject.transform.position = hitInfo.point;
             currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
         }
     }
 
     private void RotateFromMouseWheel()
     {
         Debug.Log(Input.mouseScrollDelta);
         mouseWheelRotation += Input.mouseScrollDelta.y;
         currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
     }
 
     private void ReleaseIfClicked()
     {
         if (Input.GetMouseButtonDown(0))
         {
             currentPlaceableObject = null;
 
         }
     }
 
     private void TreeZero()
     {
         i = 0;
         
     }
     private void TreeOne()
     {
         i = 1;
         
     }
     private void TreeTwo()
     {
         i = 2;
         
     }
     private void TreeThree()
     {
         i = 3;
         
     }
 
 
 }

In all honesty I am a beginner at coding and I feel like its a pretty obvious mistake. Thank you.

Comment
Add comment · Show 1
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 Ady_M · Jan 06, 2020 at 06:43 PM 0
Share

There is nothing preventing it from destroying the object and then re-creating it on the next frame, many times a second.

Could you explain exactly how you want it to function? Are you supposed to position the mouse and then press the mouse button once? Are you supposed to be able to press and drag the mouse to place multiple trees? Or is that supposed to simply drag the newest tree around until the mouse button is released? etc. Go into details, please.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

133 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

Related Questions

Grey Screen? 2 Answers

Editor Camera moving while rotating 0 Answers

Changing Skybox tint in game, permanently changes it in editor? 1 Answer

Sprites Leaving Trails Glitch? 0 Answers

Why is this code glitching? 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