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 /
  • Help Room /
avatar image
0
Question by Ifeelfine · Aug 25, 2015 at 07:51 PM · gameobjectinputbugpositioning

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;
     }
 }
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

26 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

Related Questions

Ledge Climb (Sort of working) Need help 0 Answers

Camera Controller default position? 0 Answers

Scale gameobjects position depending on screen size like if it was a UI element 0 Answers

Snap object to grid made from cubes during runtime 0 Answers

Gameobject moving too far on Input 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