- Home /
Change a float value with the horizontal axis?
I got a touch panel in which there is the TouchPad script (form Unity Standard Assets) . This script only allows the horizontal axis to be changed (min is -1 and max is 1). I want to change a float value (smoothly) to -180 when at minimum and 180 when at maximum. This variable is used in a camera rotation script (which works fine).
How can I do this?
Answer by ma22be61 · Aug 20, 2017 at 09:41 PM
Nevermind , I got it. It was simpler than I thought.
All you have to do is check if the horizontal axis is greater or lower than 0. A script example down here.
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using UnityEngine;
public class increaseNumByTouch: MonoBehaviour {
float horizontal;
float thenumberyouwanttoincrease;
void Update () {
horizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
if (horizontal > 0) {
thenumberyouwanttoincrease += 5;
} else if (horizontal < 0) {
thenumberyouwanttoincrease += -5;
}
}
}
You can also set the maximum to 180 if you wish but I don't really need it anymore.
You may also want to consider multiplying thenumberyouwanttoincrease by Time.deltaTime to eli$$anonymous$$ate framerate dependence and smooth the rotation further. Ins$$anonymous$$d, it would then rotate by degrees (presumably) per second.
Furthermore, you might also simply choose to use the Horizontal axis values as a multiplier ins$$anonymous$$d. For an example of this:
// Degrees per second for rotation
public float degPerSec = 180.0f;
void Update()
{
horizontal = CrossPlatformInput$$anonymous$$anager.GetAxis ("Horizontal");
thenumberyouwanttoincrease += horizontal * degPerSec * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Adapting camera movement to touch input 0 Answers
CrossPlatformInputManager.GetAxis() always returns 0 5 Answers
Can't solve hitbox issues in mobile game 1 Answer
Touch and Swipe at the same time mobile 0 Answers
Moving the cube on a surface. 1 Answer