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 UnholyCathar · Sep 21, 2015 at 10:18 PM · gameobjectinput2d gamepositiontutorials

Gameobject moving too far on Input

Hey all, I have this sorta weird problem with a 2D game I'm trying to make. I've been using the 2DRoguelike Tutorial as a base for a small 2D dungeon puzzle game and for some reason my Player Game Object doesn't seem to be acting like it should be.

My main problem seems to be when I'm moving the player object around. My movement scrip it basically the same as the one in the tutorial, but for some reason whenever I move the player in a direction, it moves slightly further than it should (instead of moving by 1, it moves by 1.2 approximately) which can sorta screw up the dynamic of the game.

I think the possible problems may be either:

1) My sprites are clunkily set up and something's going wrong (I had to scale the width and height of some sprites to 2 because I had them at 50x50px but the tutorial uses 100x100px) Would this be an issue?

2) I haven't implemented the "player turn" section of the tutorial because there are no enemies and I didn't want the player to waste time (the game is a time based puzzle solver); I'll try implementing that quickly to see if that fixes any bugs but I'm not sure it will.

I can post my code if people want, but it's pretty much the same as the tutorial.

Thanks in advance!

//EDIT: Here's the code for my PlayerController.cs, it's a member of the abstract class MovingObject.cs, which is the exact same as in the tutorial.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MovingObject {
 
 
     //small delay between each puzzle, timer starts after delay finishes
     //also reset player position
     public float restartLevelDelay = 1f;
 
     private Animator anim;
 
     private float time;
     //only 1 item per puzzle, if i have it, can do other half
     private bool hasItem;
 
     //maybe put in game manager?
     private Vector2 playerSpawnPoint;
 
     // Use this for initialization
     protected override void Start () 
     {
         anim = GetComponent<Animator>();
 
         //when I have the pieces, let me do the second half of the puzzle
         hasItem = false;
         time = 10;
         playerSpawnPoint = new Vector2(4, 0);
 
         base.Start ();
     }
 
     private void OnDisable()
     {
         //maybe not needed
         //anything I want to happen when the level is over?
     }
     
     // Update is called once per frame
     void Update () {
 
         int horizontal = 0;
         int vertical = 0;
 
         horizontal = (int)Input.GetAxisRaw("Horizontal");
         vertical = (int)Input.GetAxisRaw ("Vertical");
 
         if(horizontal != 0)
             vertical = 0;
 
         //TODO: FIX THIS
         if(horizontal != 0 || vertical != 0)
         {
             //Debug.Log(horizontal + " " + vertical);
             AttemptMove<Wall>(horizontal, vertical);
             anim.SetBool ("IsWalking", true);
         }
 
         //time -= Time.deltaTime;
         CheckIfGameOver();
     
     }
 
     protected override void AttemptMove <T>(int xDir, int yDir)
     {
         Debug.Log (yDir);
         base.AttemptMove <T>(xDir, yDir);
 
         RaycastHit2D hit;
 
         if(Move (xDir, yDir, out hit))
         {
             //do thing
             Debug.Log ("Moving");
         }
     }
 
     private void OnTriggerEnter2D (Collider2D other)
     {
         Debug.Log("trigger hit");
         if(other.tag == "Item")
         {
             Debug.Log ("Got Item");
             hasItem = true;
             other.gameObject.SetActive(false);
         }
         else if (other.tag == "Exit")
         {
             Invoke ("Restart", restartLevelDelay);
             enabled = false;
         }
     }
 
     protected override void OnCantMove <T>(T component)
     {
         Wall hitWall = component as Wall;
 
         //Crate hitCrate = component as Crate;
         //crate.PushCrate(transform.position.x, transform.position.y);
     }
 
     private void Restart()
     {
         Application.LoadLevel (Application.loadedLevel);
     }
 
     private void CheckIfGameOver()
     {
         if(time <= 0)
         {
             enabled = false;
             GameManager.instance.GameOver();
         }
     }
 }
 
 /*
     -Player moves by one space on the grid when input is made
         -Set it so that there is a check to see if the player is moving.
             If he is(moving to new tile), make it so he can't move;
             Else, move in input direction.
     -Player can push blocks
     -Player can pick up items
     -Player can use items he has
     -Player can stand on switches 
  */
 

Comment
Add comment · Show 2
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 bubzy · Sep 21, 2015 at 10:57 PM 0
Share

better to post the code :)

avatar image UnholyCathar bubzy · Sep 21, 2015 at 11:23 PM 0
Share

I've posted the code for my PlayerController.cs, which is where I think the problem is occurring :)

In update, there's a commented out Debug.Log() that shows horizontal and vertical, which should both be ints, but for some reason when I checked them, their values were 1.2 or 1.1 or something

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by UnholyCathar · Sep 22, 2015 at 12:06 AM

FIXED! (sort of)

Turns out the transform.position of the player sprite was for some reason adding 0.2 or 0.3 to whatever the "Vector2 start" variable in the Move function of MovingObject.cs

I'm guessing this was caused by my clunky sprite work.

I fixed the problem by typecasting the x and y of the transform.position of the gameobject to ints so they don't have the excess excess float values. Shown below:

 Vector2 start = new Vector2((int)transform.position.x, (int)transform.position.y);

This is pretty roundabout so if anyone finds a better solution or some way I can fix the clunky sprites, let me know! :)

Comment
Add comment · Share
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

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

33 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

Related Questions

how to save positions in Vector3 Array und move playerobjekt to the saved position in array's?? 1 Answer

KillPlayer, Null Reference Exception. 2 Answers

Assign a prefab to gameobjects 2 Answers

Player Win and Display on-screen restart options and start menu 2D game 0 Answers

ARKit Point Clouds are prioritized over my GameObjects? 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