Quantcast
Channel: Questions in topic: "input.getaxis"
Viewing all 266 articles
Browse latest View live

Check Input for android? Raycast or hittest

$
0
0
hello i am making a Platform Run game for android and you can move by tapping on guiTexture buttons on the screen but sometimes when you press the button it wont respond then you have to tap again to make it respond. i am currently using Hittest to check the input. is raycasting more reliable? please help me, Vince

PlayStation Vita input

$
0
0
Hello, recently I have got the PSVITA version of unity to test with my psVita. But i have no clue on what to use in my c# scripts for input. I have looked online but found no results, so please could someone refer me to a place where i can look at all the inputs like square button and analog sticks. And will the default axis "Horizontal" and "Vertical" work with PSVita. Thanks, Harry.

How to convert Input.GetAxis to Accelerometer control?

$
0
0
var steerValue = Mathf.Clamp(Input.GetAxis("Horizontal"), -1, 1); var forwardValue = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1); var reverseValue = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0); var handbrakeValue = Mathf.Clamp(Input.GetAxis("Jump"), 0, 1); above code working for car on key events I want to change this for Android accelerometer special for steerValue.

Slerp camera rotation moves on it's own at runtime?

$
0
0
I have a script to rotate the camera with an input axis, it works mostly. The big problem is that it starts moving on run time without input, and when I use the input, it eventually resists going further, and drifts back to centre. Trying to figure out how to stop this, so the input axis just rotates the camera normally without restriction, but does nothing when no input is used...? I am beginner to C#, but am trying my hardest to learn. there is a rigid body attached to the camera, set to no gravity, no drags, and mass of 1. the axis inputs are set to gravity and sensitivity 3, and dead at 0.005. the camera has one child, which is a light. Any input is appreciated! Danke! ; D using UnityEngine; using System.Collections; public class control_rotator : MonoBehaviour { //rotate variables public float horizontalSpeed = 0.3F; public float verticalSpeed = 0.3F; public float speed = 0.3f; public Transform target; // set to the main camera (itself) //movement variables public float moveSpeed = 3.0f; void Update() { // input and transform rotation values float h = horizontalSpeed * Input.GetAxis("Horizontal2"); float v = verticalSpeed * Input.GetAxis("Vertical2"); transform.Rotate(-v, -h, 0); //locate and slerp rotation values Vector3 relativePos = target.position - transform.position; Quaternion where = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, where, Time.deltaTime * speed); // input and transform movement values float moveHorizontal = moveSpeed * Input.GetAxis ("Horizontal"); float moveVertical = moveSpeed * Input.GetAxis ("Vertical"); //movement vectors and physics Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody.velocity = movement * moveSpeed; } }

Transform input.Getaxis into touch input

$
0
0
Hello everybody, I want to convert input.Getaxis ( horizontal or vertical ) into touch inputs (or at least mouse inputs for tests). I set up some guiTextures, and checked touch input on them. The problem is that they only responde one time, and you must re-touch the guitexture to move again, its not continuous. This is the control for the inputs : if (Input.touchCount > 0) { // Get the touch info Touch t = Input.GetTouch(0); if (t.phase == TouchPhase.Began) { // Are we touching the left arrow? if (guiLeft.HitTest(t.position, Camera.main) ) { h = -1; } if (guiRight.HitTest(t.position, Camera.main)) { h = 1; } if (guiJump.HitTest(t.position, Camera.main)) { k = 1; } } } else if (Input.GetMouseButtonDown (0)) { // Are we clicking the left arrow? if (guiLeft.HitTest (Input.mousePosition, Camera.main)) { h = -1; } if (guiRight.HitTest (Input.mousePosition, Camera.main)) { h = 1; } if (guiJump.HitTest (Input.mousePosition, Camera.main)) { k = 1; } } else { h = Input.GetAxis ("Horizontal"); k = Input.GetAxis ("Vertical"); } These are the formulas I use for the movements if(h * rigidbody2D.velocity.x < maxSpeed) rigidbody2D.AddForce(Vector2.right * h * moveForce); if (k * rigidbody2D.velocity.y < maxSpeed) { rigidbody2D.AddForce (Vector2.up * k * moveForce); } Thank you for your help

Animations Not Working

$
0
0
Howdy, There has been a problem in my game for about two weeks now and it has been driving me insane. I have a BasicAnimations javascript I wrote that is supposed to play a walking animation when walking, and two jumping animations, one after another, when jumping. The problem is that when I'm doing a running jump, none of the animations play when the jumping animations are supposed to play. When I'm not doing a running jump and just jumping up and down, the jumping animations play regularly. Like I've said, I've been studying this script for two weeks, and I still don't know how to solve it. I know it has something to do with the jumping animations and walking animation playing at the same time, but I'm not sure how to fix it. I've tried everything, and I am tearing my hair out. Thanks. #pragma strict var grounded : boolean; grounded = true; function Update () { if (Input.GetAxis("Horizontal") && grounded == true) { animation.Play("walk"); } if (Input.GetAxis("Vertical") && grounded == true) { animation.Play("walk"); } if (Input.GetButtonDown("Jump") && grounded == true) { grounded = false; animation.Play("jump start", PlayMode.StopAll); animation.PlayQueued("jump end", QueueMode.CompleteOthers); } if (grounded == false) { animation.Stop("walk"); grounded = true; } }

rigidBody2d.rotation only updating if rigidbody is moving.

$
0
0
I'm working on a simple twin stick shooter control script. All is working fine.. when the character is moving, if you let go of the left stick, the right stick will no longer rotate the sprite. using UnityEngine; using System.Collections; public class player_movement : MonoBehaviour { public int playerNumber; public Vector2 rightStick; private float angularVelocity; private float radialDeadZone; public Vector2 direction; private float currentRotation; public float speed; // Use this for initialization void Start () { rightStick = new Vector2(0,0); angularVelocity = 12.0f; radialDeadZone = 0.1f; } // Update is called once per frame void FixedUpdate () { //Debug.Log("RSx " + rightStick.x + " RSy " + rightStick.y + " LSy " + Input.GetAxis("LStickY_"+playerNumber) + " RSx " + Input.GetAxis("LStickX_"+playerNumber)); float moveHorizontal = Input.GetAxis ("LStickX_"+playerNumber)*speed; float moveVertical = Input.GetAxis ("LStickY_"+playerNumber)*speed; rigidbody2D.velocity = new Vector2(moveHorizontal, moveVertical); rightStick = new Vector2(Input.GetAxis("RStickY_"+playerNumber), Input.GetAxis("RStickX_"+playerNumber)); direction = new Vector3(rightStick.x, rightStick.y); if(direction.magnitude > radialDeadZone) { Debug.Log(Mathf.Atan2(direction.x,direction.y)* Mathf.Rad2Deg); currentRotation = Mathf.Atan2(direction.x,direction.y)* Mathf.Rad2Deg; rigidbody2D.rotation = Mathf.Lerp(transform.rotation.x, currentRotation, Time.deltaTime* 360); } } } Am I missing something to do with rigidbody2d? I'm feeding rigidbody2d.rotation a float and rigidbody2d.velocity a vector2.

xbox controller analog stick as digital input

$
0
0
I am working on a game where i want the left stick to do movement normally, so Input.GetAxis("") works perfectly. For the right stick, however, I want input to be digital, much more akin to button presses. Axis would work if i could have perfect readings from GetAxis(), infinite gravity and infinity sensitivity. but this is not realistic.

Player is overjumping (jumping at least twice when it shouldn't)

$
0
0
Hi, I am at the moment coding the movement script for my player object, but I've encountered a problem where the player jumps like twice, making him jump really high, I think it's because I make the player jump if he is grounded and the left joystick goes above 0.8, so sometimes it inputs twice before leaving the ground, is there any workaround for that? here's my code: using UnityEngine; using System.Collections; public class PlayerMovementScript : MonoBehaviour { Rigidbody2D body2D; Transform myTransform; bool facingRight = true; public Transform graphicsObject; public Transform swordObject; float direction; public float walkVelocity; public float lerpVelocity; public float jumpVelocity; bool isJumping; void Start () { body2D = GetComponent(); myTransform = GetComponent(); } bool isGrounded () { RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, -Vector2.up, 1.05f); foreach (RaycastHit2D hit in hits) { if (hit.transform != myTransform) { return true; } } return false; } void Update () { direction = Mathf.Lerp(direction,Input.GetAxis("HorizontalLeft"), lerpVelocity * Time.deltaTime); direction = Mathf.Clamp(direction, -1f, 1f); if (isGrounded()) { if (Input.GetButtonDown("Jump") || Input.GetAxis("VerticalLeft") >= 0.8f) { body2D.velocity += new Vector2(0f, jumpVelocity); } } body2D.velocity = new Vector2(direction * walkVelocity, rigidbody2D.velocity.y); if (swordObject.localRotation.z <= 0.7f && swordObject.localRotation.z >= -0.7f) { graphicsObject.localScale = new Vector3 (1f, graphicsObject.localScale.y, graphicsObject.localScale.z); } else { graphicsObject.localScale = new Vector3 (-1f, graphicsObject.localScale.y, graphicsObject.localScale.z); } } void OnGUI () { GUILayout.Label(facingRight.ToString() + " " + swordObject.localRotation.z.ToString("F2") + " isGrounded " + isGrounded()); } } Thanks in advance, btw, the inputs are set for gamepads, that's the reason why I want to use left joystick to jump and give the option to also use a button.

how to get MouseLook to stay strictly in either X or Y planes

$
0
0
Hi, I am trying to modify the MouseLook script to force the mouse motion to stay strictly in the X or Y planes rather than be able to tilt. Here's my code: if (Input.GetAxis("Mouse X")){ transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0); } if (Input.GetAxis ("Mouse Y")){ transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, 0, 0); } Of course this doesn't work because Input.GetAxis returns a float and I need a boolean value. Is there any way to address Input.GetAxis to find out whether it is using Mouse X or Mouse Y at any given moment. Thanks. Zaffer

How to trigger "Vertical Axis" in script? (C#)

$
0
0
Hi Unity, I have tried all sorts of ways to get my character to move forward via script. My goal is to, when both mouse 1 and mouse 0 are pressed, have him move forward. His input controls are already set up in the player control script, so all I need to do is figure out how to activate the "Vertical Axis" when I press both mouse buttons. Any suggestions? if (Network.peerType == NetworkPeerType.Disconnected) if (Input.GetMouseButton (0) && Input.GetMouseButton (1)) { { //Move Forward } Edit: I have tried Input."Vertical Axis" = 1 and = true, but none of those methods are correct it seems.

How can i get the same input as “Input.GetButton ("Horizontal”)” and “Input.GetButton (“Vertical”)“ using dual joystick?

$
0
0
Hi, I have used Input.GetAxis("Horizontal”) & Input.GetAxis (“Vertical”) to move my aircraft and Input.GetButton ("Horizontal”) & Input.GetButton (“Vertical”) to rotate my aircraft, then i wanted to convert it for mobile device, so i have used DualJoystick by importing “Standard Assets(Mobile)” , and i can move using dual joystick , but i am not able to rotate , so please help me , how can i convert “Input.GetButton ("Horizontal”)” and “Input.GetButton (“Vertical”)“ into dual joystick input.

How to get overall intensity for each Joystick Axis?

$
0
0
As you all know, `Input.GetAxis("Horizontal")` and `Input.GetAxis("Vertical")` each return a value between -1 and 1, which is great for calculating things like walk speed. I want to get one number from both of these axes combined, between 0 and 1 that is equal to how far the joystick is being pressed in any direction. Essentially, I want a normalized number for two axes. `Mathf.Max(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));` doesn't work, since any diagonal position will just return 0.5f, 0.5f (doesn't approach 1). Adding each axis together and dividing by two doesn't work for the same reason. If anyone can help me with an equation that will return 1 for the Joystick pressed all the way in any direction (up/down, left/right and any diagonal), 0.5 for halfway, 0 for not-pressed, etc. I would be grateful. Thanks!

Stop walk sound when not grounded?

$
0
0
Hey guys, i need your help for a code, i have my script for FootSteps sound, works great, but i have a little issue, when i jump and walk at the same time, the sound don't stop, i need it to play only the on ground, can you guys help me? #pragma strict //Set your variable for the footstep audioclips var footstepsWood : AudioClip; var footstepsMetal : AudioClip; var footstepsStone : AudioClip; var character : CharacterController; //This script requires that an audio source component is attached to your First Person Controller @script RequireComponent(AudioSource); function Awake(){ character = GetComponent(CharacterController); } function LateUpdate () { //Check whether the user is moving the player if(character.isGrounded){ if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) { // if the audio is not playing then play - this stops it playing multiple clips at thye same time if (!audio.isPlaying) { audio.Play(); } } else { audio.Stop(); } } } function OnControllerColliderHit (hit : ControllerColliderHit) { // if your character controller is on a floor piece tagged as woodFloor then it sets the audioClip as footstepsWood and sets the volume to 90% if (hit.gameObject.tag == "woodFloor") { audio.clip = footstepsWood; audio.volume = 1.0; } else if (hit.gameObject.tag == "metalFloor") { audio.clip = footstepsMetal; audio.volume = 1.0; } else if (hit.gameObject.tag == "stoneFloor") { audio.clip = footstepsStone; audio.volume = 1.0; } }

Will not rotate when going different speed?

$
0
0
So, I have a 2d sprite that needs to move forward when the vertical axis is greater then 0. This works fine. I also made it so that the sprite cannot rotate unless moving forward. But now, I want to have the sprite change speed when shift or control is pressed. But here's the problem. When I press shift everything works great, the sprite goes double the speed and can rotate. But when I press control, it cuts the speed in half, like it is supposed to, but I cannot rotate what so ever. Note it is all in C#. using UnityEngine; using System.Collections; public class Car : MonoBehaviour { public float speed; float actualSpeed; public float rotationSpeed; float actualRotationSpeed; void Start () { } void Update () { if (Input.GetKey (KeyCode.RightShift)) { actualSpeed = speed * 2; } else if (Input.GetKey (KeyCode.LeftShift)) { actualSpeed = speed * 2; } else if (Input.GetKey (KeyCode.RightControl)) { actualSpeed = speed / 2; } else if (Input.GetKey (KeyCode.LeftControl)) { actualSpeed = speed / 2; } else { actualSpeed = speed; } if (Input.GetAxis ("Vertical") > 0) { actualRotationSpeed = rotationSpeed; } if (Input.GetAxis ("Vertical") < 0) { actualRotationSpeed = -rotationSpeed; } if (Input.GetAxis ("Vertical") == 0) { actualRotationSpeed = 0; } } void FixedUpdate(){ transform.Translate (Vector3.up * Input.GetAxis("Vertical") * actualSpeed); transform.Rotate (new Vector3(0, 0, -1), Input.GetAxis("Horizontal") * actualRotationSpeed); } }

Joystick not working

$
0
0
Hi everyone, I'm using a Speedlink Rocketeer ViabratioN joystick with Unity3d. When calibrating buttons & axis at the control panel, everything works fine. At Unity, the buttons works fine too but the results of the GetAxis method of the Unity Input class aren't correct. I'm debugging axis with a GUIText The configuration of this input at the Input Manager is: --Gravity: 1.93 --Dead: 0.12 --Sensitivity: 4.16 Unity only recognizes one of the sense-movement, I mean, at the origin, value starts at 0 but suddenly changes to -1, and if I move forward the value still being -1, but if I move backwards value changes incrementally to 1. It looks like a bad calibration, but the calibration at the panel control is fine.

Replace Input.GetAxis("Mouse X") with touch?

$
0
0
Hi, How to replace Input.GetAxis("Mouse X") with touch input? I copied script wich works very well with mouse input but when I run it on andriod it is not working too good. Thanks for your advice :)

Two if statements true while it shouldn't be, wtf ?!

$
0
0
Hi guys, I'm using an enumeration linked to a script on the main camera. This enum is an index to know in which level of zoom I am (choices are BASE, ROOM or CHAR). In my ZoomRoomScript attached to a room object, I handle an OnMouseDown event which zoom in from the BASE to the ROOM. And we you scroll back, the camera goes from ROOM to BASE. I also got a ZoomCharScript, which is very similar to the ZoomRoomScript, except its linked to a character object and zooms are between ROOM and CHAR. My problem is that in the 2 scripts, I do have differents condition to check the Scroll back, which is based on the zoom level of the Main Camera. void Update() //ZoomRoomScript { if (Input.GetAxis ("Mouse ScrollWheel") < 0 && Camera.main.GetComponent ().GetCurrentZoom () == CameraScript.ZoomLevel.ROOM) { Debug.Log("1"); // Some code } } void Update() //ZoomCharScript { if (Input.GetAxis ("Mouse ScrollWheel") < 0 && Camera.main.GetComponent ().GetCurrentZoom () == CameraScript.ZoomLevel.CHAR) { Debug.Log("2"); // Some code } } So when I'm on the ZoomLevel.CHAR on the Main Camera, and scroll back, only the if-statement of the ZoomCharScript should be true, given the statement. But it's not. I always got both Debug.Log... I don't understand why, and I'm stucked on this for two days. If anyone have a hint/solution to this, that would be very kind. Best regards

Changing Walkspeed for Run Button - Help!

$
0
0
Hi Im new to Unity3D and recently came from Game Maker to Unity3D for more game making options. I am stuck on making a Run Button for my 2D platformer, I am using Javascript. I have some idea on how it all works, But I am still new to Unity so I need to get a custom to all its functions it has to offer. I have a run button to increase the walkspeed to a higher value by pressing a button say like left shift or something while moving left or right, although I am using my Xbox controller for this so it would be the whole "Input.GetButton("Fire1) and "Input.GetAxis("Horizontal")" Although I am confused. Because I have rewritten this script out many times in many different spots and searching around I have been told to use it under the Function Update(). But it still does not work. I have tried making it its own function to no avail. Nor will it work under the normal movement (Even though the speed defiantly increased just it did it by default so it bought me to square one). And I am absolutely stumped at this and would require some assistance as I have looked around all over the internet or to my knowledge I have and have found no solutions. This is my code: { if (movement.enabled && Input.GetButton("Fire1")&& Input.GetAxis("Horizontal")) movement.runSpeed = 10 * movement.walkSpeed; // < ---- RUNNING } I am however using this script with Javascript, I am using a character controller AND it is under the "function Update()" section. And Im sorry if I make some of you slap your heads in disbelief if this is simple or if the code isn't great. I just would like some guidance or at least a step in the right direction. Again, I am new. Thank you for reading this and hope you help me out as soon as possible.

How to stop a rigidbody2D when using Inpt.GetAxis("Horizontal") ?

$
0
0
I have a rigibody2D component on my character and I'm using this code to move it: float runSpeed = 8.0f; void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); rigidbody2D.velocity = new Vector2(moveHorizontal * runSpeed, rigidbody2D.velocity.y); } What I would like is even though I'm holding the arrow key (or moving the joystick), if I press the attack button, the character should stop moving until the attack animation ends. Then resume moving. I've tried the following but it doesn't seem to work: void Update() { if(Input.GetButtonDown("Melee")) { rigidbody2D.velocity = Vector3.zero; moveHorizontal = 0; runSpeed = 0; } } I added each line within the if statement one at a time of course and when didn't work I tried another. Then finally I just put all 3 in there but the character keeps moving forward while I'm holding a direction and pressing the attack button. I've also tried GetButton instead of GetButtonDown. I figure if I make runSpeed zero, it will make the x component of the vector2 zero and thus no movement in the x-axis but I guess I was wrong. This is a snippet from my script but I figured this would be enough info to solve the problem or get a suggestion but if anyone requires more info or to see more code please let me know. I'm still trying to figure it out but I hope someone can help before I go nuts. It might simpler than I'm thinking it is at the moment, like using animation states or something. But any help is appreciated. Thank you.
Viewing all 266 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>