Ideas to move across tile centers only?
Hi there,
My code will be posted below. Overall what I'm trying to achieve is a smooth movement of the player, that doesn't move into "out-of-bounds" tiles, and keeps the player at .5 of the y-axis when move left or right and .5 of the x-axis moving up and down. I've been able to achieve 2/3 of what I'm wanting. It's keeping my player centered is the problem. Basically, I want it to move on a tightrope for the neatness and because of the graphic I have on the tiles. Please take a look at my code and let me know what suggestions you may have.
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D playerRB;
public float moveSpeed = 1f;
public GameObject player;
public Tilemap outOfBounds;
//used for camera controll limits
private Vector3 bottomLeftLimit;
private Vector3 topRightLimit;
public static PlayerController instance;
// Start is called before the first frame update
void Start()
{
if (instance == null)
{
instance = this;
}
else
{
if (instance != this)
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
// Update is called once per frame
void Update()
{
//Debug.Log("movement running");
float xPos = transform.position.x;
float yPos = transform.position.y;
Vector2 newPosition;
//move right
if(Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
{
newPosition = new Vector2(xPos + .52f, yPos);
if (TileTest(outOfBounds, newPosition) == null)
{
this.transform.position = Vector2.MoveTowards(this.transform.position, newPosition, moveSpeed * Time.deltaTime);
}
}
//move left
if (Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
{
newPosition = new Vector2(xPos - .52f, yPos);
if (TileTest(outOfBounds, newPosition) == null)
{
this.transform.position = Vector2.MoveTowards(this.transform.position, newPosition, moveSpeed * Time.deltaTime);
}
}
//move up
if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") > 0)
{
newPosition = new Vector2(xPos, yPos + .52f);
if (TileTest(outOfBounds, newPosition) == null)
{
this.transform.position = Vector2.MoveTowards(this.transform.position, newPosition, moveSpeed * Time.deltaTime);
}
}
//move down
if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") < 0)
{
newPosition = new Vector2(xPos, yPos - .52f);
if (TileTest(outOfBounds, newPosition) == null)
{
this.transform.position = Vector2.MoveTowards(this.transform.position, newPosition, moveSpeed * Time.deltaTime);
}
}
}
public TileBase TileTest(Tilemap tilemap, Vector2 cellWorldPos)
{
return tilemap.GetTile(tilemap.WorldToCell(cellWorldPos));
}
public void SetBounds(Vector3 bottomLeft, Vector3 topRight)
{
bottomLeftLimit = bottomLeft + new Vector3(0.5f, 1f, 0f);
topRightLimit = topRight + new Vector3(-1f, -1f, 0f);
}
}
Your answer
Follow this Question
Related Questions
Place RandomTile from script 1 Answer
Alpha transparency works but not in the whole tile 0 Answers
Updating tilemap tile sprites? 0 Answers
My Tile Palette becomes a grey mess 3 Answers
Positioning isometric tiles with depth ontop of flat tiles 1 Answer