P
P
PopTop2021-07-27 10:49:39
Unity
PopTop, 2021-07-27 10:49:39

How to make a jump to the other side on the border of the screen?

Please don't swear too much, I'm making the first game for my daughter. Please tell me how to implement a normal jump up when the character is on the edge of the screen / on the edge of the platform. I have implemented side jumps to the side, but when the character is on the edge of the platform on the right, I want the character to jump not to the side, but up when I press the jump to the left, but when I press the jump to the left, the character jumps diagonally to the left and I want to implement the same From the left side. I honestly tried to find information for a long time.

if ((Input.GetKeyDown(KeyCode.A)) && groundDetection.isGrounded)
{
rb.AddForce((new Vector2(-1, 1)) * force, mode: ForceMode2D.Impulse);
}

if ((Input.GetKeyDown(KeyCode.D)) && groundDetection.isGrounded)
{
rb.AddForce((new Vector2(1, 1)) * force, mode: ForceMode2D.Impulse);
}
60ffba495ad91486604095.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nik Titor, 2021-07-27
@nikerAI

Perhaps I did not fully understand the question, but you can try to put a trigger, upon contact with which, on the edge of the platform, the character will change his trajectory during the jump.

A
alfss, 2021-07-27
@alfss

Place an object on the sides and if you crossed / touched it, then start moving in the opposite direction and up or where you need.

P
PopTop, 2021-07-27
@PopTop

I was prompted, but still I didn’t completely solve the problem, now when I hit the trigger, I always jump up that when I click on A and D, in the inspector you can also see that the x-axis becomes 0. How to fix this situation? It is necessary that the player can jump in one direction, and not just up
void Start()
{
rb = GetComponent();
}
public Vector2 jumpVector = Vector2.one;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("JumpRightUp"))
{
jumpVector = Vector2.up;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("JumpRightUp"))
{
jumpVector = Vector2.one;
}
}
private void Update()
{
if ((Input.GetKeyDown(KeyCode.A)) && groundDetection.isGrounded)
{
rb.AddForce((new Vector2(-1, 1) * jumpVector) * force, mode: ForceMode2D.Impulse );
}
if ((Input.GetKeyDown(KeyCode.D)) && groundDetection.isGrounded)
{
rb.AddForce((new Vector2(1, 1) * jumpVector) * force, mode: ForceMode2D.Impulse);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question