Problems with positioning game objects
Hello everyone,
I have a weird bug in my game and an issue I couldn't find anything about on here: When building a tower next to the location of a player avatar (there are two), the tower is sometimes built on the (0, 0)-position (it's a 2D-game) and doesn't register collisions correctly (the towers find their targets via a trigger collider and the towers positioned incorrectly fire on targets that are clearly outside their trigger). I tried logging the "pos"-variable and the tower's position after it's set and weirdly these positions are correct, but the tower is still spawned at (0, 0). Might there be a problem with my handling of inputs? Just to be safe I also included the movement-script for my player-avatars below since that's the only other script that uses player inputs up to now.
BuildTower-Script:
public class BuildTower : MonoBehaviour {
public GameObject prefab;
GameObject tower;
Vector3 pos;
float delay = 0.5f;
float lastTowerBuilt = 0;
public int basicTowerPrice = 50;
// Use this for initialization
void Start () {
pos = transform.position;
}
void Update () {
pos = transform.position;
if (gameObject.GetComponent<Properties>().playerTag == 1)
if(Input.GetKeyUp(KeyCode.Q)) {
if(Time.time - lastTowerBuilt > delay && GetComponent<Gold>().gold >= basicTowerPrice){
RaycastHit2D hit = Physics2D.Raycast(transform.position + new Vector3(-.6f, 0), new Vector2(-1, 0), .5f);
if(!hit.collider || hit.collider.isTrigger){
tower = Instantiate (prefab);
tower.transform.position = pos + new Vector3(-1, .5f, 0);
tower.GetComponent<Properties>().playerTag = 1;
lastTowerBuilt = Time.time;
PayForTower(basicTowerPrice);
}
}
}
if (gameObject.GetComponent<Properties>().playerTag == 2)
if(Input.GetKeyUp(KeyCode.M)) {
if(Time.time - lastTowerBuilt > delay && GetComponent<Gold>().gold >= basicTowerPrice){
RaycastHit2D hit = Physics2D.Raycast(transform.position + new Vector3(.6f, 0), new Vector2(1, 0), .5f);
if(!hit.collider || hit.collider.isTrigger){
tower = Instantiate (prefab);
tower.transform.position = pos + new Vector3(1, .5f, 0);
tower.GetComponent<Properties>().playerTag = 2;
lastTowerBuilt = Time.time;
PayForTower(basicTowerPrice);
}
}
}
}
void PayForTower(int price){
GetComponent<Gold> ().gold -= price;
}
}
PlayerMovement-Script: using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f; // The speed that the player will move at.
Vector3 movement; // The vector to store the direction of the player's movement.
Rigidbody2D playerRigidbody; // Reference to the player's rigidbody.
void Awake ()
{
// Set up references.
playerRigidbody = GetComponent <Rigidbody2D> ();
}
void FixedUpdate ()
{
float h;
float v;
// Store the input axes.
if ( GetComponent<Properties>().playerTag == 1 ) {
h = Input.GetAxisRaw ("Horizontal1");
v = Input.GetAxisRaw ("Vertical1");
} else {
h = Input.GetAxisRaw ("Horizontal2");
v = Input.GetAxisRaw ("Vertical2");
}
// Move the player around the scene.
Move (h, v);
}
void Move (float h, float v)
{
// Set the movement vector based on the axis input.
movement.Set (h, v, 0);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
transform.position += movement;
}
}