- Home /
Start Button
how can i make this script start after pressing button like tap to start if i put it on Update or Start when i press the button its so slow using UnityEngine;
public class PlayerController : MonoBehaviour {
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public void FixedUpdate ()
{
rb.AddForce (0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey ("d")) {
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey ("a")) {
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetMouseButton (0)) {
if (Input.mousePosition.x > Screen.width / 2)
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
else
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
Answer by mani3307 · Mar 27, 2018 at 07:14 AM
@Miller-M4 , What I understand from your question is you want to use a UI BUTTON to start the game,you can definetily do this refer this tutorial 1.)UI-button tutorial 2.)StartMenuTutorial Implement these tutorials in your required way and if you got any doubts ping me I will help for sure
i Fixed it thank you now the player have to press on screen to start but can you tell me does game get paused while showing ads cuz my restart button show ads while showing ads i can press on screen and player moving while showing ads
while the adds are showing, you may have to put a script telling the game to be paused. For example, canceling all controls like putting your controls under a bool like this. I am not a great programmer but I will try to help your problem. using UnityEngine,Ui; using UnityEngine;
Pause $$anonymous$$enu : $$anonymous$$onoBehaviour { public bool isControllable = true; public bool AddPlaying = false;
void Update () { if(AddPlaying = true) { isControllable = false; } }
void Pause () { // create pause function // put up your add isControllable = false; AddPlaying = true; }
}
//in your controls function you could use a if statement to see if iscontrollable is true. If it is, you can move your character. something like this.
Answer by Miller-M4 · Mar 27, 2018 at 08:44 PM
i tried a lot but nothing working this is my script now every thing is good expect when i press restart while ads showing i press on screen player start move and ads still showing and player crash and it repeating like this and can someone apply it to my script cuz i'm bad using UnityEngine; using UnityEngine.Advertisements; using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour {
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public bool Started = false;
public GameObject StartText;
void Start()
{
Started = false;
}
public void FixedUpdate ()
{
if (Input.GetMouseButtonDown (0)) {
StartText.gameObject.SetActive (false);
Started = true;
}
if (Started == true) {
Time.timeScale = 1f;
Time.fixedDeltaTime = 0.02f * Time.timeScale;
rb.AddForce (0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey ("d"))
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
{
if (Input.GetKey ("a"))
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);{
}
}
}
if (Input.GetMouseButton (0)) {
if (Input.mousePosition.x > Screen.width / 2)
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
else
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
public void Restart()
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
Time.timeScale = 1f;
Time.fixedDeltaTime = 0.02f * Time.timeScale;
Advertisement.Show ("video");
}
}`
Your answer
Follow this Question
Related Questions
How to constantly add force to my player when a button is hold? 2 Answers
AddForce adding together on 2 button presses 1 Answer
how do I use a Raycast to apply force to the player on collision (in 2d)? 0 Answers
Increase Rigidbody2D Velocity based on player direction 1 Answer
Making player move based on mouse position universal for all screens 1 Answer