Question by
marianaloa · Oct 21, 2021 at 10:24 AM ·
2d gamecharacter2d-physicsjumping
Problems with jumping in another scene
so my player works correctly in 2 scenes that i made first, but in the other 3 and if i do a new one it jumps too high, idk what's the problem, i'm new in this stuff and i followed a tutorial.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
private Rigidbody2D rb2D;
private Animator animate;
private float moveSpeed;
private float jumpForce;
private bool isJumping;
private float moveHorizontal;
private float moveVertical;
private bool facingRight = true;
// Start is called before the first frame update
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
animate = gameObject.GetComponent<Animator>();
moveSpeed = 2f ;
jumpForce = 60f;
isJumping = false;
}
// Update is called once per frame
void Update()
{
moveHorizontal = Input.GetAxisRaw("Horizontal");
moveVertical = Input.GetAxisRaw("Vertical");
animate.SetFloat("Speed", Mathf.Abs(moveHorizontal));
//animate.SetFloat("JumpSpeed", Mathf.Abs(moveVertical));
if(moveHorizontal > 0 && !facingRight)
{
Flip();
}
else if (moveHorizontal < 0 && facingRight)
{
Flip();
}
}
void FixedUpdate()
{
if(moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
}
if (!isJumping && moveVertical > 0.1f )
{
animate.SetTrigger("Jump");
rb2D.AddForce(new Vector2(0f, moveVertical * jumpForce), ForceMode2D.Impulse);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Platform")
{
isJumping = false;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Platform")
{
isJumping = true;
}
}
void Flip()
{
facingRight = !facingRight;
Vector2 currentScale = transform.localScale;
currentScale.x *= -1;
transform.localScale = currentScale;
}
}
,
Comment
Your answer
Follow this Question
Related Questions
Player can only jump once!? 0 Answers
Broken Jump Physics 0 Answers
Differentiating similar components attached to a game object in script? 0 Answers