I am designing a shooting game in Unity 3d for which I have a requirement that only when an image is tapped the shooting should occur. Please check the command used for the purpose:
if (Input.GetMouseButtonDown(0)){
if (!clickedConfirmed){
GameObjectClicked = GetClickedGameObject();
clickedConfirmed = true;
}
Currently, shooting occurs when clicked anywhere on the screen. How can activation of shooting be bonded only to the gameobject (image) instead of being activated when clicked anywhere on the screen.
Your need to give more context in your example, especially what GetClickedGameObject() is actually doing internally.
As such I'm limited in what I can say is the problem, but I suspect that you aren't using GameObjectClicked correctly.
I would expect that you'd do a check such as:
this.gameObject == GameObjectClicked
Note that if this being used as a method of developing a UI there are more elegant solutions out there, including the long awaited new GUI system included in the Unity3d 4.6 open beta.
Related
I am using image (Amazon Icon) as button with Event Trigger Pointer Click component but it's not working when I click.Sometimes it's working while clicking one specific place at icon but I don't know what is triggering it.Is there any way to make it work? I am not able to use button because of my canvas is small and it's being blurry when I use buttons.
pcinteract script:
`
public void start(){
number++;
Debug.Log("Clicked:" + number);
}
Screenshot from game screen
Inspector panel of Amazon Icon
working area
It appears that there may be an issue with the event trigger component on the Amazon icon game object. To troubleshoot this, you can try the following steps:
Verify the presence of a collider component on the Amazon icon game object. If it is not present, add it so that clicks can be detected.
Ensure that the "Is Trigger" option within the collider component has been checked.
Scrutinize the order of components in the event trigger component, making certain that the "Pointer Click" component is above the others.
Consider incorporating a Debug.Log() statement into the start method, to determine if the method is being
called upon clicking the Amazon icon.
If these steps prove ineffective, you may want to experiment with creating a new game object and adding an image component, collider component, and event trigger component to it. Test clicking the new object to see if it functions as intended.
I have a script that recognizes swiping input. When I now start a swipe, it also triggers Input.GetMouseButtonDown(0). Can you tell me how to delete Input.GetMouseButtonDown(0) that comes from the swipe?
With that swipe the game gets paused. (with Time.timeScale = 0f;)
After resume, the action which was triggered from Input.GetMouseButtonDown(0) before pausing the game goes on.
My Ideas: Work with TouchPhase.Stationary or look for short tap?
Off the top of my head, you can either:
Call Input.ResetInputAxes; it will clear all input flags for one frame.
Set a flag that a swipe was handled, and check for it when checking for Input.GetMouseButtonDown(0) elsewhere.
Both will require ensuring that the swipe detection happens before you check for Input.GetMouseButtonDown(0).
One way of ensuring it, is by using Unity's script execution order settings.
I would recommend not using Input class at all, it causes many problems, better have separate RaycastTarget on UI with Script implementing IPointerDownHandler, IPointerUpHandler, that will ensure only one RaycastTarget at the moment is handling your touches. Input.GetMouseButton will return true even if some RaycastTarget already should have consumed your events.
I just started exploring Unity and I'm making a game in UNITY 2D.
I decided to start with a simple menu screen to get used to UNITY.
I made the menu and moved on to programming the buttons.
However when I drag the script (or the game object holding the script) in to the button on click section and try and select my function it does not show up.
My unity window
Manager in the button on click script.
My code in C# (this is my first time using c#)
what I want to know is: have I done something wrong; doe unity work differently to how I understand it; is unity doing something weird; how do I fix it.
(appologies for the weird post layout I don't know why it did that).
Just insert a ; at the end of your code and you should be fine. You should notice that the compiler is complaining about the missing semicolon. Also, make sure to attach your script to the _Manager GameObject.
You need to :
add a semi colon to the end of the line in the Buttons class
Add Buttons Class to the _Manager gameobject in the scene
I've made a simple animation for my dishwashing machine in 3dsmax. I imported the object as an fbx file into Unity. The animation is called "openandclose". The animation itself opens and closes the machine.
However I can't seem to find a way to trigger the animation to happen once at the click of a button. How can I do this?
Unity has a great documentation regarding every core function including animations. Check this link out: http://docs.unity3d.com/462/Documentation/ScriptReference/Animation.Play.html
and to get the button pressed:
http://docs.unity3d.com/462/Documentation/ScriptReference/Input.GetKeyDown.html
combined it would be something like this i guess:
void Update() {
if (Input.GetKeyDown("e"))
animation.Play("openandclose");
}
So my basic 2d game framework in SlimDX is going well. I've created a custom Sprite object, and have some rudimentary mastery of rendering sprites. I just need to move them now.
I have a basic game loop set up, which is based on an old C# game programming book. Having set up all the graphics device bits and bobs, and drawn my sprite on screen, I enter a method in my Game class called Run(). This has the following content:
public void Run()
{
while( this.Created )
{
// Process one frame of the game
ProcessFrame();
Render();
// Handle all events
Application.DoEvents();
}
}
ProcessFrame() is supposed to have all the game logic in it, in response to events. Whilst I have a simple event handler (based on an override of OnKeyDown) set up to detect keypresses, I'm wondering how to collect keypresses such that I can process the responses to them in the ProcessFrame() method.
Some initial research on this subject suggests creating a HashSet<Keys>. I've never done this before, so I'm not sure how I would go about it.
There are probably many ways to do this, so I thought I'd see what people would recommend before jumping right in there.
Thanks in advance everyone.
I don't believe you need to manually capture inputs. This typically would be handled by the direct input keyboard class. I personally have not used SlimDX myself yet but as it is a wrapper for DirectX (which I've dabbled in) you should just need to initialize a variable to hold the reference to the keyboard so that you can poll it for changes, and then act upon those changes.
http://slimdx.org/docs/#T_SlimDX_DirectInput_Keyboard
Such that you would code something like
if (myKeyboard.IsKeyDown(Keys.Escape)) { ProgramStatus = GameState.Title; }
assuming my reference variable was called myKeyboard.
Edit:
I might also mention that depending on the particular library your using that it can be beneficial to save current, and previous keyboard states. This is very useful when you want to ensure that the program only takes in one keystroke vs many.
XNA as I recall has a KEYDOWN method for controllers, useful to test if the button is being held down. If you need to take in a input for each time the button is hit capture both.
IE if your library has a Keypress method, your good, if it captures KEYDOWN only you may need to capture current and previous.
if (myMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)