Hi, im creating an 2D game and everything until i created main menu. When i enter from my game scene it works good but when i enter from my main menu scene and press play it starts to jump too high… If i now go and choos game scene it does same thing but when i click my player game object in hirearchy it stops jumping high and works good… After that when i play my game scene again it works good… So in conclusion if i Use main menu scene it doesnt work, if i use game scene after pressing play at main menu scene it doesnt work. If i use ONLY game scene without previously opening main menu scene it works. Very wierd, i hope you understand In this game my player is always jumping when grounded. I dont know if you need scripts aswell so im going to put them all here.
PlayerMotor:
public float jumpPower = 10.0f;
public float speed = 2.0f;
Rigidbody2D myRigidbody;
bool isGrounded = false;
// Start is called before the first frame update
void Start()
{
myRigidbody = transform.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (isGrounded)
{
myRigidbody.AddForce(Vector3.up * (jumpPower * myRigidbody.gravityScale * 20.0f));
}
if (Input.GetKey(KeyCode.LeftArrow))
{
myRigidbody.velocity = new Vector2(speed * -1, myRigidbody.velocity.y);
}
//else
//{
// myRigidbody.constraints = RigidbodyConstraints2D.FreezePositionX;
// }
if (Input.GetKey(KeyCode.RightArrow))
{
myRigidbody.velocity = new Vector2(speed, myRigidbody.velocity.y);
}
// else
// {
// myRigidbody.constraints = RigidbodyConstraints2D.FreezePositionX;
// }
if (!Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
{
myRigidbody.constraints = RigidbodyConstraints2D.FreezePositionX;
myRigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
else
{
myRigidbody.constraints = RigidbodyConstraints2D.None;
myRigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionStay2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = false;
}
}
CameraMovement:
Transform CamTransform;
public Transform Player;
public float followspeed;
{
CamTransform = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
Vector3 targetPosition = new Vector3(Player.position.x, CamTransform.position.y, CamTransform.position.z);
CamTransform.position = Vector3.Lerp(CamTransform.position, targetPosition, Time.deltaTime * followspeed);
}
MainMenu:
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame ()
{
Application.Quit();
}