- Home /
move object back and forth with joystick input
I use joystick mechanic. What I want to do is ; Translating my object to forward when my joystickInput.magnitude is increasing, and translating to backward when my joystickInput.magnitude is decreasing. I need to know if my joystickInput.magnitude is increasing or decreasing, but I couldnt find any idea for how to do it. I tried to keep previous magnitude for comparing them, but since there is an equality condition in the 2. if statement, after magnitude = joystickInput.magnitude , it goes into this if statement again and again, so in every frame it just goes into else statement for once and almost never goes to the (-)direction.
If I dont add the equality sign, then it goes into else statement again and again as the same as previous above version.
private void Update() { joystickInput = new Vector2(joystick.Horizontal, joystick.Vertical);
if(joystickInput.magnitude > 0.1f && joystickInput.magnitude < 0.9f)
{
Debug.Log("joystickInput.magnitude " + joystickInput.magnitude);
Debug.Log("magnitude " + magnitude);
if (joystickInput.magnitude >= magnitude)
{
Debug.Log("increasing");
magnitude = joystickInput.magnitude;
foreach (GameObject obj in spheres)
{
Vector3 direction = (obj.transform.position - centerPoint.position);
obj.transform.position += direction * (joystickInput.magnitude + 2) * Time.deltaTime;
}
}
else
{
Debug.Log("decreasing");
magnitude = joystickInput.magnitude;
foreach (GameObject obj in spheres)
{
Vector3 direction = (obj.transform.position - centerPoint.position);
obj.transform.position += -direction * (joystickInput.magnitude + 2) * Time.deltaTime;
}
}
} here I added a gif from the game scene. the disintegration and integration mechanic is working like that. Every sphere in the scene is represented as obj in the script. (Ignore the forward down movement)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I make a joystick that aims and shoot 0 Answers
I have this problem when animating my character? 0 Answers
Transform Object over Players head before destroying 0 Answers