Unity3D Input Manager And Using Buttons For Input.GetAxis - c#

I just bought NGUI and need some help...
Right now my 2D character is moving left and right with the left and right arrows.
I have the left and right arrows setup to do this in the input manager.
I made left and right arrow buttons with NGUI added them to my UI Root.
How do I get it to mimic the left arrow being pressed when I press the left arrow button?
Or is there a way to add the left arrow button being pressed to the input manager?
All I want to do is make the left arrow button act like when I press the left arrow key and the same for the right.
If there is another way to do it without NGUI thats fine too.
I just want to be able to move left and right when I press the left and right buttons on a mobile device.
I have been stuck on this for like 6 hours now, and can't figure it out.
Any help would be greatly appreciated!
Thanks!
Heres an example of how I'm moving the character
float h = Input.GetAxis("Horizontal");
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
if (Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);

The effect of the button is processed in the OnPress (bool isDown) function. By default, it is called everytime you hover over the button.
If I understood you correctly, you want to simulate this hovering while pressing the related arrow key on the keyboard.
In order to do this, you would have to call the OnPress (bool isDown) function yourself. Try to add this line to your code:
SendMessage("OnPress", true);
This will call the OnPress function on all scripts added to the gameobject.
To call the function on a special gameObject just add a public variable go and then go with go.SendMessage("OnPress", true);
Hope, this will help. :)

Related

How do I move an object from off screen to just off screen? (Unity2D)

I have an enemy that can constantly move quite far away when the player moves too much, so I want to move it to the just off screen so it can move back into view without appearing suddenly. It would also be nice to have them reappear in the same direction from where they left. Currently have this just to check if .isVisible was working;
if(GetComponent<Renderer>().isVisible == false)
{
Debug.Log("Off Screen");
this.transform.Translate(0, distanceFromPlayer - 5, 0);
}
Having a constant distance wouldn't work because it would either appear on screen as if it was teleporting or appear too far off screen and not make it back. I understand how Camera.WorldToScreenPoint works, just couldn't find a way to make them appear in the same direction.
Look into Camera.ViewportToWorldPoint
So if you wanted to put them outside the left of the screen, you could do
cam.ViewportToWorldPoint(new Vector3(0 - enemyScreenOffset,.5,cam.nearClipPlane));
or if you wanted the upper right side of the screen
cam.ViewportToWorldPoint(new Vector3(1 + enemyScreenOffset,1,cam.nearClipPlane));
It depends on how your game works, but you might want to change the Z component of the Vector3 to be the enemyPosition so they don't change height.

Unity drag and drop check right or left (2D)

I wanna build a card game in Unity.
I want to player swipe cards left or right but I wanna show some tricks about what's gonna happen if they swipe left or right so if they grab the card (2D Image) and go a bit left or right a text will appear on image depends on side.
I tried to use swipe and use X axis change but It's not working properly.
Here is a basic pictured information about what i wanna build:
I would advise using the drag handlers system in unity to track where the card started, and trigger different effects at different distances / directions from its origin.
You can detect the drag of the card by implementing IBeginDragHandler, IDragHandler, IEndDragHandler on a script on your card object.
Begin is called when the object starts dragging, Drag is called during each frame its moving, and End is called when the object is dropped.
When a drag begins, you can cache the position where the card started. Then in Drag handler each frame you can check the distance of your card from where it started. Once it has moved more than a certain distance, you can trigger whatever tricks or effects you want to show.
Depending on how your coordinate space is set up, you could for example know left from right during drag updates such as:
// Inside of your on drag method, check each frame for where the card is
// If it has moved far enough, trigger an effect
if (Vector3.Distance(dragStartPosition, transfom.position) > yourTriggerDistance) {
// if X is greater, the card moved right
if (transform.position.x > dragStartPosition.x) {
// Trigger Right swipe text
ShowSomeRightSwipeText();
}
// otherwise, X is smaller, so the card has moved left by the required distance
else {
// Trigger left swipe text
ShowSomeLeftSwipeText();
}
}
Also please remember that:
In order for these interfaces to work, you need to have a Physics 2D Raycaster attached to your camera in the scene.
You need to make sure you have an Event System in the scene as well.
The above two things are often asked issues here regarding detecting object dragging.

How to make a UI element draggable ony on Y axis in Unity?

So, I am making an android app about some locations in my city. In my UI I have background, title, description and a selector tab, that should be dragged out and dragged back down whenever user wants it to. Here is an example:
https://imgur.com/a/kVSMc4U
The problem is - I don't really know how to make an event that checks when the object is being dragged in +Y or -Y axis and dragg's it to the fingers position. Any suggestions?
You can add event trigger to UI panel
1. UI panel, add component -> Event -> Event Trigger
2. Add new event type Drag and add the function on it
public void onDragPanel()
{
if (!_isDrag)
{
_isDrag = true;
if (Input.GetAxis("Mouse Y") > 0 )
//Move up
else if (Input.GetAxis("Mouse Y") < 0 )
//Move down
}
}
and for the up/down movement, you can use animation with two animation states or you can use UITWEEN from asset store.
https://assetstore.unity.com/packages/tools/animation/ui-tween-38583
Let me know if you have any further queries.
Good luck!

Why is my object not continuously rotating with button press?

I have a space ship that will rotate clockwise or counter-clockwise depending on if the left or right button is pressed. I was using a virtual joystick and it was working fine but decided to change to left and right buttons. Now if click a button it will rotate to a fix position and stop each time i press the button, I'd like for it to move continuously in one direction while button held down and stop when released.
I'm using the unity standard assets with the cross platform input ButtonHadler script in conjunction with my "move" script.
void Update()
{
if (CrossPlatformInputManager.GetButtonDown("turn"))
{
TurnShip();
}
}
public void TurnShip()
{
transform.Rotate(Vector3.up * 50f * Time.deltaTime);
}
You're using GetButtonDown, which returns true only once when you press the button, and remains false until you release the button and press it again. Use GetButton instead.
Changing to GetButton was part of what I was missing but I had also set up my buttons up incorrectly I had accidentally added one event type with two function a pointer down and pointer up in the same event type instead of separating them.

Unity Virtual Reality: Question about input

What I am trying to achieve at this point, is the following:
Let's say that we have a Cube game object and a SteamVR player.
The Cube object is on position y = 100 and the SteamVR player is on position y = 0.
I want to make it possible that the player can zoom in on the game object by doing the following:
-> Pressing both triggers and bringing the controllers close to each other will zoom in.
-> Pressing both triggers and bringing the controllers away from each other will zoom out.
I think u understand the effect that I want to create.
For my project I am using the SteamVR Unity plugin.
Could someone say me if this is possible and give me some insight on how to do this?
Thanks in forward.
Have an if statement checking for two inputs and move the camera position in the forward direction close and closer to the target. If you want to save the original camera position before zooming just store the Camera.main.forward before incrementing it.
pseudocode
public SteamVR_Input_Sources LeftInputSource = SteamVR_Input_Sources.LeftHand;
public SteamVR_Input_Sources RightInputSource = SteamVR_Input_Sources.RightHand;
public Vector3 currentZoom;
public Vector3 zoomAmount;
void update(){
if( SteamVR_Actions._default.Squeeze.GetAxis(LeftInputSource) && SteamVR_Actions._default.Squeeze.GetAxis(RightInputSource)){
currentZoom.forward += zoomAmount.forward; //increment zoom by whatever amount while
triggers are held
Camera.main.transform.forward = currentZoom;
}
}
I have not tested this, hence why I labeled it pseudocode, however, I hope this helps!

Categories