I am trying to build a button that changes the game speed when it is being pressed and changes it back when it stops being pressed.
To make this I wrote this functions:
public void OnPointerDown()
{
mouseDown = true;
Background1.sprite = on;
Time.timeScale = 2;
}
public void OnPointerUp()
{
Time.timeScale = 1;
mouseDown = false;
Background1.sprite = off;
}
They are already connected to the button. When the button is pressed the game speed changes, but never changes back (it should happen when I stop holding the button).
Know how to fix this?
Related
I have a problem in my project. I created an animation to open the door and made it open whenever a button was pressed. Then I copied the animation and entered its value -1 to make the door close after 2 seconds, but if the character presses the door open button again while playing the closing animation, it plays that animation and this causes a bug in the game. I apologize for telling a little complicated. I am waiting for your answers. Here is my code ->
public Text text;
public Animator anim;
private void Start()
{
text.enabled = false;
anim = GetComponent<Animator>();
}
private void Update()
{
if(text.enabled && Input.GetKeyDown(KeyCode.E))
{
anim.Play("DoorOpen");
}
}
private void OnTriggerExit(Collider other)
{
text.enabled = false;
}
private void OnTriggerEnter(Collider other)
{
text.enabled = true;
}
}
You shouldn't use the Animator Play() method and instead create parameters like a Trigger that.. well... triggers the animation.
Also, you don't say what's the error you're getting so it is hard to help you, but I guess the animation is restarting? You can set so the animations can't be transitioned to itself, so it won't restart if triggered again before finishing. Edit your AnimatorController asset, and click on the animation that shouldn't transition to itself, then uncheck where it says precisely that: "Can transition to itself". (Maybe you have to double click it, maybe you have to click on the transition, don't really remember right now, you'll figure it out :) ).
This is the script taken from the tanks asset store and I want to convert the shooting which is currently being done by space bar to Ui Shoot button. It should fire the bullet.
Like I want that when my button is pressed it should charge as to how much velocity is imparted to the bullet and it soon as I release it the bullet should be fired.
Also, I was thinking of a fill image which can show that thing in the button as well.
public class TankShooting: MonoBehaviour
{
//public int m_PlayerNumber = 1; // Used to identify the different players.
public Rigidbody m_Shell; // Prefab of the shell.
public Transform m_FireTransform; // A child of the tank where the shells are spawned.
public Slider m_AimSlider; // A child of the tank that displays the current launch force.
//public AudioSource m_ShootingAudio; // Reference to the audio source used to play the shooting audio. NB: different from the movement audio source.
//public AudioClip m_ChargingClip; // Audio that plays when each shot is charging up.
//public AudioClip m_FireClip; // Audio that plays when each shot is fired.
public float m_MinLaunchForce = 15f; // The force is given to the shell if the fire button is not held.
public float m_MaxLaunchForce = 30f; // The force is given to the shell if the fire button is held for the max charge time.
public float m_MaxChargeTime = 0.75f; // How long the shell can charge for before it is fired at max force.
private string m_FireButton; // The input axis that is used for launching shells.
private float m_CurrentLaunchForce; // The force that will be given to the shell when the fire button is released.
private float m_ChargeSpeed; // How fast the launch force increases, based on the max charge time.
private bool m_Fired; // Whether or not the shell has been launched with this button press.
private float nextFireTime;
private void OnEnable()
{
// When the tank is turned on, reset the launch force and the UI
m_CurrentLaunchForce = m_MinLaunchForce;
m_AimSlider.value = m_MinLaunchForce;
}
private void Start ()
{
// The fire axis is based on the player number.
m_FireButton = "Fire1"; //+ m_PlayerNumber;
// The rate that the launch force charges up is the range of possible forces by the max charge time.
m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
}
public void Update()
{
// The slider should have a default value of the minimum launch force.
m_AimSlider.value = m_MinLaunchForce;
// If the max force has been exceeded and the shell hasn't yet been launched...
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
{
// ... use the max force and launch the shell.
m_CurrentLaunchForce = m_MaxLaunchForce;
Fire (m_CurrentLaunchForce, 1);
}
// Otherwise, if the fire button has just started being pressed...
else if (Input.GetButtonDown (m_FireButton))
{
// ... reset the fired flag and reset the launch force.
m_Fired = false;
m_CurrentLaunchForce = m_MinLaunchForce;
// Change the clip to the charging clip and start it playing.
//m_ShootingAudio.clip = m_ChargingClip;
//m_ShootingAudio.Play ();
}
// Otherwise, if the fire button is being held and the shell hasn't been launched yet...
else if (Input.GetButton (m_FireButton) && !m_Fired)
{
// Increment the launch force and update the slider.
m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
m_AimSlider.value = m_CurrentLaunchForce;
}
// Otherwise, if the fire button is released and the shell hasn't been launched yet...
else if (Input.GetButtonUp (m_FireButton) && !m_Fired)
{
// ... launch the shell.
Fire (m_CurrentLaunchForce, 1);
}
}
public void Fire (float launchForce, float fireRate)
{
if (Time.time > nextFireTime)
{
nextFireTime = Time.time + fireRate;
// Set the fired flag so only Fire is only called once.
m_Fired = true;
// Create an instance of the shell and store a reference to it's rigidbody.
Rigidbody shellInstance =
Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
// Set the shell's velocity to the launch force in the fire position's forward direction.
shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward;
// Change the clip to the firing clip and play it.
//m_ShootingAudio.clip = m_FireClip;
//m_ShootingAudio.Play ();
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
}
}
There is Very Easy Solution for this:
Create A Function In Your Script
ShootTheBullet()
and The Create A UI Button and Click on Add Button In your Button's Component Event
"On Click"
Drag and Drop The gameObject that you have attached your C# script From Hierarchey that Contains "ShootTheBullet()" and then click On Right Option Select your Script an Select That Function.
Now you are ready to Shoot Bullets With UI Button.
So this should be a simple answer but I cannot seem to find it anywhere. In Unity, I have a toggle box I want to turn off when the game starts, but unlike most game objects or text, SetActive does not work with toggle boxes. What is the command to turn the graphic off?
public void StartGame () {
mainText.SetActive (false);
startButton.SetActive (false);
StartCoroutine (SpawnBalls ());
//Turn off toggle box graphic
playing = true;
}
public void AddBall () {
if (ballBox.isOn) {
ballNumber = 3;
//Debug.Log ("yes");
} else {
ballNumber = 2;
}
If I understand you correctly you want to be able to remove the display of the toggle on the start of the game? If so set a reference to the toggle you want to click on and off heres a small example:
Toggle T = GameObject.Find("myToggle").GetComponent<Toggle>();
T.gameObject.SetActive(false);
Make a reference to it and you should be able to click it of and on as you need it tested it out on a game I'm working and it worked for me let me.
Setting: Creating my first multiplayer game and running into an odd issue.
it's a tank game where players can shoot bullets and kill each other.
You can charge the bullets to shoot it faster/further away.
Problem:
When the client player charges fully and releases, the bullet continue to be spawned repeatedly and never stops. This issue doesn't occur if the client player does Not charges fully.
I believe the issue is the update function within the if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
Note:
The host player does not have this problem, therefore it's somehow related to networking.
private void Update()
{
if (!isLocalPlayer)
return;
// Track the current state of the fire button and make decisions based on the current launch force.
m_AimSlider.value = m_MinLaunchForce;
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) {
m_CurrentLaunchForce = m_MaxLaunchForce;
CmdFire ();
} else if (Input.GetButtonDown (m_FireButton) && !m_Fired) {
m_Fired = false;
m_CurrentLaunchForce = m_MinLaunchForce;
m_ShootingAudio.clip = m_ChargingClip;
m_ShootingAudio.Play();
} else if (Input.GetButton (m_FireButton)) {
m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
m_AimSlider.value = m_CurrentLaunchForce;
} else if (Input.GetButtonUp(m_FireButton)) {
CmdFire ();
}
}
[Command]
private void CmdFire()
{
// Set the fired flag so only Fire is only called once.
m_Fired = true;
// Create an instance of the shell and store a reference to it's rigidbody.
GameObject shellInstance = (GameObject)
Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) ;
// Set the shell's velocity to the launch force in the fire position's forward direction.
shellInstance.GetComponent<Rigidbody>().velocity = m_CurrentLaunchForce * m_FireTransform.forward;
// Change the clip to the firing clip and play it.
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play ();
NetworkServer.Spawn (shellInstance);
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
If you look trough the documentation you will find this -> [Command] functions are invoked on the player object associated with a connection. This is setup in response to the "ready" message, by passing the player objec to the NetworkServer.PlayerIsReady() function. The arguments to the command call are seriialized across the network, so that the server function is invoked with the same values as the function on the client.
I think the reason why it wasnt working for you is because you have to pass an argument to the function like
[Command]
private void CmdFire(bool m_Fired)
{
// Set the fired flag so only Fire is only called once.
m_Fired = true;
// Create an instance of the shell and store a reference to it's rigidbody.
GameObject shellInstance = (GameObject)
Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) ;
// Set the shell's velocity to the launch force in the fire position's forward direction.
shellInstance.GetComponent<Rigidbody>().velocity = m_CurrentLaunchForce * m_FireTransform.forward;
// Change the clip to the firing clip and play it.
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play ();
NetworkServer.Spawn (shellInstance);
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
And then call it like this:
CmdFire(m_Fired) where m_Fired has to point to the players own variable.
I have an FBX object with animation. The object is a box with animation for opening. What I'm trying to do is when the user clicks a button the box will open (play open animation) and when the button is clicked again the box will close (play open animation backwards).
While the opening animation is playing and I click the button again the opening animation stops and the box starts to close, that works fine.
The problem is that when the animation is finished (open) and then I click the button to close, the animation is not playing and it just jumps to a closed box without animation.
Here is my code:
public class ClickBtn : MonoBehaviour {
public GameObject box = null;
bool reverse = false;
private void OnMouseDown()
{
Debug.Log(reverse);
if (!reverse)
{
box.animation["Take 001"].speed = 1;
}
else
{
box.animation["Take 001"].speed = -1;
}
reverse = !reverse;
box.animation.Play("Take 001");
}
}
your animation.WrapMode is set wrong (probably WrapMode.Once, which is default). In your case you could use:
WrapMode.PingPong: Ping Pong's back and forth between beginning and end.
animation.wrapMode = WrapMode.PingPong;
Mind you, you don't need
box.animation["Take 001"].speed = -1;
anymore, this is done automatically.
When animation is finished its time is reseted to beginning.
Simple workaround set time to end before playing backwards.
public GameObject box;
bool direction = false;
private void OnMouseDown()
{
Debug.Log(direction);
if (!direction)
{
box.animation["Take 001"].speed = 1;
}
else
{
box.animation["Take 001"].speed = -1;
//if animation already finisihed, set time to end before playing it backwards
if(!box.animation.isPlaying){
box.animation["Take 001"].time =box.animation["Take 001"].clip.length;
}
}
direction = !direction;
box.animation.Play("Take 001");
}