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
*/
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
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! :)
Your answer
Follow this Question
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