- Home /
Can't teleport on the X axis
Good... time period that you're in. I have an issue, I'm trying to make my character teleport in different directions, I'm able to teleport up and down just fine, but I have no idea how to teleport on the X axis. Please help, here's my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour{
public float Yincrement;
public float Xincrement;
private Vector2 targetPos;
private void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow)) {
targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
transform.position = targetPos;
}
else if (Input.GetKeyDown(KeyCode.DownArrow)) {
targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
transform.position = targetPos;
if (Input.GetKeyDown(KeyCode.RightArrow)) {
targetPos = new Vector2(transform.position.y, transform.position.x + Xincrement);
transform.position = targetPos;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
targetPos = new Vector2(transform.position.y, transform.position.x - Xincrement);
transform.position = targetPos;
}
}
}
}
Comment
Best Answer
Answer by Magso · Dec 05, 2020 at 12:28 AM
You did the Y axis correctly
targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
But you then swapped them around for the X axis
targetPos = new Vector2(transform.position.y, transform.position.x + Xincrement);
Vector2 is a struct that takes (X value, Y value) so it needs to be
targetPos = new Vector2(transform.position.x + Xincrement, transform.position.y);
Answer by Z1cro · Dec 05, 2020 at 03:10 PM
THANK YOU SO MUCH! Also, I had to remove the X intercerpt script from the Y one and make two separate scripts.