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 /
  • Help Room /
avatar image
0
Question by matrixx_ · Jan 20 at 05:24 PM · 2d game

Object keeps bobbing between two tiles

I'm trying to make a tower defense game where you only have a set amount of towers, meaning there is no buying and selling towers, and on of the core pieces of the game is being able to move your tower. although whenever I go to move my tower, on sperate frames the tower keeps moving between two positions (grid tiles), even when i'm not moving

  • Only GetTower() and FiveFrameUpdate() are relevant to the tower moving

  • Player has several static arrays with stats, and position

  • LevelController has a list of spots where a tower is placeable

.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class InputController : MonoBehaviour {
 
     public static int tabViewType = 1; // 0 = none, 1 = compact, 2 = full
 
     public GameObject eKeycap; // 0 = e, 1 = 1, 2 = 2, 3 = 3, 4 = 4
     public GameObject interactText;
     public bool eActive;
     public static int eIndex;
 
     [SerializeField]
     private GameObject towerMenuGO;
     private TowerMenu towerMenu;
 
     [SerializeField]
     private GameObject levelControllerGO;
     private LevelController levelController;
 
     private Tower towerScript;
     public bool moveActive;
 
     void Awake() {
         towerMenu = towerMenuGO.GetComponent<TowerMenu>();
         levelController = levelControllerGO.GetComponent<LevelController>();
     }
     
     // Start is called before the first frame update
     void Start() {
         InvokeRepeating("FiveFrameUpdate", 0f, 0.2f);
     }
 
     // Update is called once per frame
     void Update() {
         if(Input.GetKeyDown(KeyCode.Tab)) {
             switch(tabViewType) {
                 case 0:
                     tabViewType++;
                     break;
                 case 1:
                     tabViewType++;
                     break;
                 case 2:
                     tabViewType = 0;
                     break;
                 default:
                     tabViewType = 0;
                     break;
             }
         }
 
         if(Input.GetKeyDown(KeyCode.E) && TowerMenu.active) {
             towerMenu.DeactivateMenu();
         } else if(Input.GetKeyDown(KeyCode.E) && eActive) {
             towerMenu.ActivateMenu(eIndex);
         }
 
         if(Input.GetKeyUp("1") && TowerMenu.towerMenuActive) {
             towerMenu.ToUpgradeMenu();
         } else if(Input.GetKeyUp("2") && TowerMenu.towerMenuActive && !moveActive) {
             moveActive = true;
             GetTower(eIndex);
         }
 
         if(Input.GetKeyDown("2") && moveActive) {
             moveActive = false;
             Player.updateSortingLayer = 2;
         }
 
         if(Input.GetKeyDown("1") && TowerMenu.upgradeMenuActive) {
             towerMenu.UpgradeTower(0, eIndex); // damage
         } else if(Input.GetKeyDown("2") && TowerMenu.upgradeMenuActive) {
             towerMenu.UpgradeTower(1, eIndex); // attack speed
         } else if(Input.GetKeyDown("3") && TowerMenu.upgradeMenuActive) {
             towerMenu.UpgradeTower(2, eIndex); // range
         } else if(Input.GetKeyDown("4") && TowerMenu.upgradeMenuActive) {
             // towerMenu.UpgradeTower(3, eIndex); // ability
         }
     }
 
     public void ShowKeycap(int index) {
         float offsetX = 0;
         float offsetY = 0;
         
         eActive = true;
         eIndex = index;
 
         // the reason it wasn't working was because on each of the 5 tower gameObjects each had a tower script attached and
         // the tower that you were close to was calling ShowKeycap(), but the other 4 towers were calling HideKeycap(), which
         // was why the position was changing but the keycap wasn't showing, same with TowerRadius
         eKeycap.SetActive(true);
 
         if(Tower.pos[index].x > 0) {
             offsetX = -1f;
         } else if(Tower.pos[index].x < 0) {
             offsetX = 1f;
         }
 
         eKeycap.transform.position = new Vector3(Tower.pos[index].x + offsetX, Tower.pos[index].y + offsetY, 0);
         interactText.transform.position = new Vector3(eKeycap.transform.position.x, eKeycap.transform.position.y - 0.75f, 0f);
     }
 
     public void HideKeycap(int index) {
         if(index == eIndex) {
             eKeycap.SetActive(false);
             eActive = false;
             towerMenu.DeactivateMenu();
         } else {
             return;
         }
     }
 
     void GetTower(int index) {
         string tag = "Tower" + index;
         GameObject towerGO = GameObject.FindGameObjectWithTag(tag);
         towerScript = towerGO.GetComponent<Tower>();
         
         Player.updateSortingLayer = 0;
     }
 
     void FiveFrameUpdate() {
         if(moveActive) {
             towerMenu.DeactivateMenu();
 
             float currentSpot;
             float closestSpot = Mathf.Infinity;
             Vector3 closestVector = new Vector3(0f, 0f, 0f);
             Vector3 finalVector = new Vector3(0f, 0f, 0f);
             bool onTower;
 
             // bobs up and down, throughout different frames
 
             for(int i = 0; i < levelController.spots.Length; i++) {
                 onTower = false;
                 currentSpot = Vector3.Distance(Player.pos, levelController.spots[i].position);
                 if(currentSpot <= closestSpot) {
                     closestVector = levelController.spots[i].position;
                     for(int j = 0; j < 10; j++) {
                         if(closestVector == Tower.pos[j]) {
                             onTower = true;
                             j = 10;
                         } else {
                             onTower = false;
                         }
                     }
 
                     if(!onTower) {
                         finalVector = levelController.spots[i].position;
                     }
 
                     closestSpot = currentSpot;
                 }
             }
 
             towerScript.transform.position = finalVector;
         }
     }
 }


Sorry for posting the entire file, it was extremely painful only trying to seperate the important parts

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

219 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

Related Questions

Please Help. How to add score on collision mobile? 2 Answers

How to make font look sharp and smooth 1 Answer

OnTriggerEnter2D gets executed repeatedly by each collided object with the same Tag 3 Answers

2D Slide Animation 0 Answers

Differentiating similar components attached to a game object in script? 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