I’m using the following code to create a teleporting effect. When the user presses a directional arrow and presses shift it triggers the teleport. The problem I’m having is when the player wants to teleport up and down. Since there is gravity in the scene it appears to shorten the distance of the teleport. Any ideas on how I could fix this?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
//A reference to the players 2D Rigidbody
public Rigidbody2D playerRidgidbody;
//Needs to be messed with so it is appropriate
//How fast the player moves
private float moveSpeed = 5;
private float jumpForce = 7;
private float dashDistance = 3;
private int dashCharges = 2;
private bool grounded;
private Vector2 playerVelocity;
// Update is called once per frame
void FixedUpdate () {
//Need to change KeyCode to Custom input.
//Moves player to the right
if (Input.GetKey (KeyCode.RightArrow)) {
if(Input.GetKeyDown(KeyCode.LeftShift) && dashCharges > 0){
Vector2 temp = transform.position;
temp.x += dashDistance;
transform.position = temp;
} else {
playerRidgidbody.velocity = new Vector2 (moveSpeed, playerRidgidbody.velocity.y);
}
}
//Moves player to the left
else if (Input.GetKey (KeyCode.LeftArrow)) {
if(Input.GetKeyDown(KeyCode.LeftShift) && dashCharges > 0){
Vector2 temp = transform.position;
temp.x -= dashDistance;
transform.position = temp;
} else {
playerRidgidbody.velocity = new Vector2 (-moveSpeed, playerRidgidbody.velocity.y);
}
} else {
playerRidgidbody.velocity = new Vector2(0, playerRidgidbody.velocity.y);
}
if (Input.GetKey (KeyCode.UpArrow) && Input.GetKeyDown (KeyCode.LeftShift) && dashCharges > 0) {
Debug.Log("up dash");
Vector2 temp = transform.position;
temp.y += dashDistance;
transform.position = temp;
} else if(Input.GetKey (KeyCode.DownArrow) && Input.GetKeyDown(KeyCode.LeftShift) && dashCharges > 0) {
Debug.Log("down dash");
Vector2 temp = transform.position;
temp.y -= dashDistance;
transform.position = temp;
}
//Used to detect if the player is grounded
RaycastHit2D hit = Physics2D.Raycast (transform.position, -Vector2.up, .5f);
//Debug.DrawRay (transform.position, -Vector2.up, Color.red, .5f);
if (hit.collider.tag == "Ground") {
//Debug.Log ("Grounded");
grounded = true;
} else {
grounded = false;
}
if (Input.GetKey (KeyCode.Space) && grounded == true) {
//Debug.Log ("Pressing space");
playerRidgidbody.velocity = new Vector2(playerRidgidbody.velocity.x, jumpForce);
}
}
}