How can I make this code work properly? It's old but all I could find. I'm trying to make a mania type of game and I need the 4 buttons to work properly. I've tried to change the code somehow and managed to get the button be pressed down, but the thing is that the button won't go back up, they just stay as they are, pressed down. If there are any people who've done something similar to this please help and even those who've not please help too. Thanks.
private SpriteRenderer theSR;
public Sprite defaultImage;
public Sprite pressedImage;
public KeyCode keyToPress;
// Start is called before the first frame update
void Start () =>
theSR = GetComponent<SpriteRenderer>();
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(keyToPress))
{
theSR.sprite = pressedImage;
}
if(Input.GetKeyDown(keyToPress))
{
theSR.sprite = defaultImage;
}
}
}
The two conditions are identical. You probably meant to negate the later to set the button's image do defaultImage, when the key "is not down".
if (! Input.GetKeyDown(keyToPress))
{
theSR.sprite = defaultImage;
}
An if-else block whould be even neater:
if (Input.GetKeyDown(keyToPress))
{
theSR.sprite = pressedImage;
}
else
{
theSR.sprite = defaultImage;
}
Related
I have two scenes in my project. There is only one award-winning video for each scene.
In first scene, Everthing is okey, but if you come to second scene to first scene, i can't give reward to users and endofRewarded panel is not visible. What is the correct method for giving reward? How can i do that?
P.s : I know the tutorial is here.
https://developers.ironsrc.com/ironsource-mobile/unity/rewarded-video-integration-unity/
But, I am beginner.
Thank you.
Here is my code for first scene :
public class RewardedMain : MonoBehaviour
{
public string appkey;
public GameObject endofRewarded, anaEkran;
// Start is called before the first frame update
void Start()
{
IronSource.Agent.shouldTrackNetworkState(true);
IronSourceEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvaibilityChangedEvent;
IronSourceEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent;
IronSourceEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent;
//IronSourceEvents.onRewardedVideoAdReadyEvent += OnRewardedVideoAdReady;
}//END START
public void showRewarded()
{
if (IronSource.Agent.isRewardedVideoAvailable())
{
anaEkran.SetActive(false);
IronSource.Agent.showRewardedVideo("MainRewarded");
}
}
void RewardedVideoAdClosedEvent()
{
IronSource.Agent.init(appkey, IronSourceAdUnits.REWARDED_VIDEO);
IronSource.Agent.shouldTrackNetworkState(true);
}
void RewardedVideoAvaibilityChangedEvent(bool available)
{
bool rewardedVideoAvailability = available;
}
void RewardedVideoAdRewardedEvent(IronSourcePlacement ssp)
{
PlayerPrefs.SetInt("totalCoin", PlayerPrefs.GetInt("totalCoin") + 150);
GameObject.Find("GoldNumberText").GetComponent<Text>().text = PlayerPrefs.GetInt("totalCoin").ToString();
SoundManager.Instance.PlayEarnGoldSound();
endofRewarded.SetActive(true);
}
I'm not quite sure what your project looks like, so I'll give some hints as per the information you've given me now.
The problem you're having is that when you get to the second scene, the objects that are hooked up to this mono script will disappear?
If so, add the following code to awake and your GameObject will exist across scenes!
DontDestroyOnLoad(gameObject);
Anyone know how i can stop this from flicking basically I'm doing a raycast and if it hits the crafting table it it shows up a text saying press e to craft but it is flickering cause its in the void update function but if anyone knows a work around that would be nice thanks!
if(hit.collider.tag == "CraftingTable")
{
if(textIsOn)
{
noshowcraftingtext();
}
else
{
showcraftingtext();
}
}
void showcraftingtext()
{
textIsOn = true;
pressEToShowCraftingTableUI.SetActive(true);
}
void noshowcraftingtext()
{
textIsOn = false;
pressEToShowCraftingTableUI.SetActive(false);
}
The main problem of your code is that you are trying to change the state after state was changed.
That's why updated isn't the case in a big project, because all such things will lead to unpredictable or logically conflicted behavior, to avoid this you should use Data driven approach instead.
But to fix exactly your issue, you need to have a function, which isn't based on the state of the object, but returns what should happen right now.
There isn't much about what's going on inside your game, but I can guess that you are firing a raycast with mouse and then checking is it craftable or not.
public void Raycaster : MonoBehavior {
public void YourClickMethod()
{
if(hit.collider.tag == "CraftingTable")
{
hit.collider.GetComponent<UserInputReceiver>().SetClicked(this);
}
}
}
And on the object which is receiving your raycast you should add this:
public class UserInputReceiver : MonoBehavior {
private bool _isEnabled = false;
//set this inside inspector
public GameObject ObjectToEnable;
private Raycaster _currentSender = null
public void SetClicked(Raycaster sender){
_isEnabled = !_isEnabled;
_currentSender = sender;
}
public void Update(){
if(_currentSender != null && _isEnabled) {
_isEnabled = Vector3.Distance(transform.position, _currentSender.transform.position) < 1f; //set the threshold based on your unit system
}
//or another object, you can put it inside public field and enable him
//gameObject.SetActive(_isEnabled);
objectToEnable.SetActive(_isEnabled); //here you can pass a reference through inspector for your canvas
}
}
This is how you can outstand this issue with a short way.
This code will set the flag based on the received input and the distance. If you need to keep enabled text forever, than remove distance check than it will enable only with a second click, without distance dependency.
I'm very new to unity and I'm making a simple tag game with car like players. However, I'm having problems with my trigger collider. When I tag the other car it kind of flickers the is tagged true/false. Here is my code.
public Color TaggedColor;
public Color NoTaggedColor;
public bool Tagged;
// Start is called before the first frame update
void Start()
{
Tagged = true;
GetComponent<Image>().color = TaggedColor;
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "P2")
{
if (Tagged == false)
{
Tagged = true;
GetComponent<Image>().color = TaggedColor;
}
else
{
if (Tagged == true)
{
Tagged = false;
GetComponent<Image>().color = NoTaggedColor;
}
}
Debug.Log("TAG!");
}
}
void GotTagged()
{
if (Tagged == false)
{
Tagged = true;
//GetComponent<Image>().color = TaggedColor;
}
}
void TaggedOtherPlayer()
{
if (Tagged == true)
{
Tagged = false;
GetComponent<Image>().color = NoTaggedColor;
}
}
I have a Second code for P2 that the only change is the start and the tag is P1. I'm not sure if this problem is something in my code or if it's the collider it's self. If anyone has any idea why this is happening I would love to hear it!
Update: I bevile the issue is coming from the Colliders because I have an empty as a child of both players that is the trigger collider. However when I tag one the other trigger goes off as well. Should I have only one trigger? And if so how would I go about doing it?
Thanks
once you tag a player SetActive(false) the empty triggers for about 1 second or so. If you had a button that whenever its pressed it changes s value from true to false the false to true if you press and hold that button it will "flicker" back and forth between true and false. Thats what's happening with your players the trigger is constantly being pulled while you're touching so make it inactive for long enough for you to not be touching anymore. I hope that makes sense.
Trying to repeat the function function OnAttack() continuously while a button is being held down.
Basically I'm looking for an equivalent to Update() { GetKeyDown() {//code }} But with the input system.
Edit: using a joystick, cant tell what button is being pressed.
Okay I solved it by using "press" in the interactions and giving that The trigger behavior "Press and release", then did
bool held = false;
Update()
{
if(held)
{
//animation
}
else if(!held)
{
//idle animation
}
}
OnAttack() {
held = !held;
}
This way if I press the button held goes to true so it repeats the animation every frame, letting go makes "held" untrue and does the idle animation
Essentially, the function you assign to the button will be triggered twice per button press, once when it is pressed (performed), and once when it is released (canceled). You can pass in this context at the beginning of your function, just make sure you are using the library seen at the top of this script.
Now you can toggle a bool on and off stating whether or not the button is pressed, then perform actions during update dependent on the state of the bool
using static UnityEngine.InputSystem.InputAction;
bool held = false;
Update()
{
if(held)
{
//Do hold action like shooting or whatever
}
else if(!held)
{
//do alternatice action. Not Else if required if no alternative action
}
}
//switch the status of held based on whether the button is being pressed or released. OnAttack is called every time the button is pressed and every time it is released, the if statements are what determine which of those two is currently happening.
OnAttack(CallbackContext ctx) {
if (ctx.performed)
held= true;
if (ctx.canceled)
held= false;
}
This is paraphrasing a solution I created for a click to move arpg mechanic.
using System.Threading.Tasks;
using UnityEngine;
[SerializeField] private InputAction pointerClickAction;
private bool pointerHeld;
void Start()
{
pointerClickAction.canceled += ClickMouseMove;
pointerClickAction.started += PointerHoldBegin;
pointerClickAction.performed += ClickMouseMove;
pointerClickAction.canceled += PointerHoldEnd;
}
private void OnEnable()
{
pointerClickAction.Enable();
pointerPositionAction.Enable();
}
private void OnDisable()
{
pointerClickAction.Disable();
pointerPositionAction.Disable();
}
public async void ClickMouseMove(InputAction.CallbackContext context)
{
while (pointerHeld)
{
DoSomething();
await Task.Delay(500);
}
}
public void PointerHoldBegin(InputAction.CallbackContext context)
{
pointerHeld = true;
}
public void PointerHoldEnd(InputAction.CallbackContext context)
{
pointerHeld = false;
}
public void DoSomething()
{
//Your Code
}
In Task.Delay() you can insert your own polling rate in milliseconds, using Task.Yield() seems to be faster than Update so I don't recommend that, you should poll at the minimum the same delay as your physics/fixed update, having a higher delay gives a performance boost if you don't need a high amount of repetitions per loop. I set mine to 500 since I don't need my character to plot its navigation that often. In regards to TC, you would set the delay to something sensible e.g the attack's animation length, or whatever the delay rate would be for how many attacks can be performed per second.
If you want your project to scale you might want to avoid as much as possible assertions(i.e if functions) in your Update/FixedUpdate/LateUpdate functions as they are executed constantly. I recommand you to read this article about coroutines https://gamedevbeginner.com/coroutines-in-unity-when-and-how-to-use-them/
You can build coroutines which act as local update() functions which are executed only when needed. This will lead you to a better organization of your code and might boost performance in some cases.
For exemple in your case you could use something like this.
bool held = false;
Update()
{
/* Whatever you want but the least assertion possible */
}
IEnumerator RenderHeldAnimation()
{
while (held)
{
// held animation
yield return new WaitForFixedUpdate(); /* Will block until next fixed frame right after FixedUpdate() function */
// yield return null /* Will block until next next frame right after Update() function */
}
}
IEnumerator RenderIdleAnimation()
{
while (!held)
{
// idle animation
yield return new WaitForFixedUpdate(); /* Will block until next fixed frame right after FixedUpdate() function */
// yield return null /* Will block until next next frame right after Update() function */
}
}
OnAttack() {
held = !held;
if (held) {
StartCoroutine(RenderHeldAnimation());
} else {
StartCoroutine(RenderIdleAnimation());
}
}
As mentioned in another answer, the context.canceled is not called when using the Press And Release interaction. As a follow up for documentation purposes as this is a top Google result, to correctly use a bool held without doing a blind toggle (held = !held) which may end up with drift, you can access context.control.IsPressed() like the following:
void OnAttack(CallbackContext context)
{
held = context.control.IsPressed();
}
I encountered the same issue and this was the method that seemed to work for me
private float _moveSpeed = 3f;
private float _moveDirection;
private void Update()
{
transform.Translate(_moveSpeed * _moveDirection * Time.deltaTime * transform.forward);
}
public void Move(InputAction.CallbackContext ctx)
{
_moveDirection = ctx.ReadValue<float>();
}
For some odd reason,the hold interaction works properly in reading the input, but I still need the update function to implement the actual logic.
Can't really complain though, it works. Although I'd love to know why it happens this way.
Hopefully this can be of help to someone.
You can use a timer for that purpose, in combination with events KeyUp and KeyDown.
Please look at the following link. It is pretty much similar to your problem.
Link
I'm wanting to have two textures switch when ever I press the space bar in my project. But when I do, nothing is happening.
can anyone see what I'm doing wrong with my logic? I had this working with switching between 2 shaders, but not it is materials, it isn't working. I'm very confused by this.
void Switch()
{
Debug.Log(gameObject.renderer.material.name);
if(Input.GetButtonDown("Jump"))
{
diffuse = false;
//if(gameObject.renderer.material == diffuse_Material)
}
else
{
diffuse = true;
}
if(diffuse == true)
{
gameObject.renderer.material = mask_Material;
print("1");
Debug.Log(gameObject.renderer.material.name);
}
else
{
gameObject.renderer.material = diffuse_Material;
print("2");
Debug.Log(gameObject.renderer.material.name);
}
My codes changes the material once, but doesn't change back again when I press the space bar again.
update
I've taken my code out of the update method and placed it in its own one. I've since modified it to be a "simple" bool check. Now when I play the game my object starts with the material I first want on it, but when I hit space, it only changed the material for a fraction of a second.
How can I do it so that on the press of a single button, my bool toggles between true and false. I thought i had it, but I guess I don't. My debug log now looks like this:
DepthMask (instance)
1
DepthMask (instance)
2
Diffuse (instance)
Diffuce (instance)
DepthMask (instance)
DepthMask (instance)
1
I think the problem is the following:
When the user presses the jump key the boolean gets set to true, the material changes and its all good. BUT then the update method gets called again, and followed by that I'm assuming your Switch method also.
Input.GetButtonDown("Jump") will now evaluate to false, causing your boolean value diffuse to become true again, and the material changes back.
Is the material supposed to toggle everytime the users presses the Jump button? If so, change your code to (the else statement can be completely removed):
void Switch()
{
Debug.Log(gameObject.renderer.material.name);
if(Input.GetButtonDown("Jump"))
{
diffuse = !diffuse;
// material only needs to change if diffuse changed
if(diffuse)
{
gameObject.renderer.material = mask_Material;
print("1");
Debug.Log(gameObject.renderer.material.name);
}
else
{
gameObject.renderer.material = diffuse_Material;
print("2");
Debug.Log(gameObject.renderer.material.name);
}
}
}
EDIT: included the entire Switch method.
You could try to compare
renderer.sharedMaterial
instead of
renderer.material
to get the non-instanced version of the material. Something like this:
public Material a;
public Material b;
void Update ()
{
if (Input.GetButtonDown("Jump"))
{
if (renderer.sharedMaterial == a)
{
gameObject.renderer.material = b;
}
else
{
gameObject.renderer.material = a;
}
}
}