Hey guys, I’m making a 2D mobile game where you boost the ballon up with a button but everytime I reach next scene balloon boost itself without pressing the button. After I press the boost button in the new scene it returns to normal. What I’m asking is how can I stop this movement when i enter new scene ? Thanks for your help.
Can you post your code (with code tags)? Both of the button’s function and whatever handles the level change. And possibly any other scripts that you think could have influence.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour
{
public float horizontalInput;
Rigidbody2D rb2d;
public float speed = 20.0f;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
rb2d.AddForce(Vector2.up * 50, ForceMode2D.Impulse);
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
if (rb2d.position.y < -14)
{
rb2d.position = new Vector2(transform.position.x, -14);
}
if (CrossPlatformInputManager.GetButton("Jump"))
{
rb2d.AddForce(Vector2.up * 2750);
}
horizontalInput = CrossPlatformInputManager.GetAxis("Horizontal");
rb2d.velocity = horizontalInput * 35 * Vector2.right;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LinkScenes : MonoBehaviour
{
public int iLevelToLoad;
public string sLevelToLoad;
public bool useIntegerToLoadLEvel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject collisionGameObject = collision.gameObject;
if(collisionGameObject.name == "Player")
{
LoadScene();
}
}
void LoadScene()
{
if (useIntegerToLoadLEvel)
{
SceneManager.LoadScene(iLevelToLoad);
}
else
{
SceneManager.LoadScene(sLevelToLoad);
}
}
}
Where is the code for the boost button?
Jump button is boost button, I just named it jump.
But you dont have a Jump function, at least not a custom one. There are a couple things i would do differently.
First, i would create that custom function for Jump and then call it in FixedUpdate.
private void FixedUpdate()
{
if (rb2d.position.y < -14)
{
rb2d.position = new Vector2(transform.position.x, -14);
}
JumpButton();
horizontalInput = CrossPlatformInputManager.GetAxis("Horizontal");
rb2d.velocity = horizontalInput * 35 * Vector2.right;
}
public void JumpButton()
{
if (CrossPlatformInputManager.GetButtonDown("Jump"))
{
rb2d.AddForce(Vector2.up * 2750);
}
}
I also changed the GetButton to GetButtonDown. Now that it is a public function outside of Update or FixedUpdate, you can call this function from a UI button now.
Hopefully that fixes it.