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
Your answer
Follow this Question
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