- Home /
How to implement android touch input joysticks for the lego microgame
So I have been messing around with customizing the Lego microgame. I am a fairly new user, and I think it is a fun way to learn the ins and outs (Plus my son is learning along with me and it keeps him interested).
In some previous projects I used android touch input by using this method: https://www.youtube.com/watch?v=EjFdZ9NjB3A
Where you add the joysticks and then code them as input to replace the input (with simpleinput from the asset store) on the player controller. So I have added the joysticks and I am looking around and the player movement seems to be a bit more complicated than what I have worked with before. Can someone please help me out with what code I would need to change in order to change the movement and look to left and right joysticks and the jump to a button on my canvas? I would really appreciate that and would be a major bonus if someone helps me understand the special movements that I can see in the controller script but am not sure how to enable or use. If it matters, I am using unity 2019.4.11f1 personal.
NOTE: In the following code from minifigcontroller.cs I tried changing Input.GetAxisRaw() to SImpleInput.GetAxisRaw() and then I get an error saying The name 'SimpleInput' does not exist in the current context. Thanks for any help.
// Handle input.
if (inputEnabled)
{
switch (inputType)
{
case InputType.Tank:
{
// Calculate speed.
var targetSpeed = SimpleInput.GetAxisRaw("Vertical");
targetSpeed *= targetSpeed > 0 ? maxForwardSpeed : maxBackwardSpeed;
if (targetSpeed > speed)
{
speed = Mathf.Min(targetSpeed, speed + acceleration * Time.deltaTime);
}
else if (targetSpeed < speed)
{
speed = Mathf.Max(targetSpeed, speed - acceleration * Time.deltaTime);
}
// Calculate rotation speed.
var targetRotateSpeed = SimpleInput.GetAxisRaw("Horizontal");
targetRotateSpeed *= maxRotateSpeed;
if (targetRotateSpeed > rotateSpeed)
{
rotateSpeed = Mathf.Min(targetRotateSpeed, rotateSpeed + rotateAcceleration * Time.deltaTime);
}
else if (targetRotateSpeed < rotateSpeed)
{
rotateSpeed = Mathf.Max(targetRotateSpeed, rotateSpeed - rotateAcceleration * Time.deltaTime);
}
// Calculate move delta.
moveDelta = new Vector3(0, moveDelta.y, speed);
moveDelta = transform.TransformDirection(moveDelta);
break;
}
case InputType.Direct:
{
// Calculate direct speed and speed.
var right = Vector3.right;
var forward = Vector3.forward;
if (Camera.main)
{
right = Camera.main.transform.right;
right.y = 0.0f;
right.Normalize();
forward = Camera.main.transform.forward;
forward.y = 0.0f;
forward.Normalize();
}
var targetSpeed = right * SimpleInput.GetAxisRaw("Horizontal");
targetSpeed += forward * SimpleInput.GetAxisRaw("Vertical");
if (targetSpeed.sqrMagnitude > 0.0f)
{
targetSpeed.Normalize();
}
targetSpeed *= maxForwardSpeed;
var speedDiff = targetSpeed - directSpeed;
if (speedDiff.sqrMagnitude < acceleration * acceleration * Time.deltaTime * Time.deltaTime)
{
directSpeed = targetSpeed;
}
else if (speedDiff.sqrMagnitude > 0.0f)
{
speedDiff.Normalize();
directSpeed += speedDiff * acceleration * Time.deltaTime;
}
speed = directSpeed.magnitude;
// Calculate rotation speed - ignore rotate acceleration.
rotateSpeed = 0.0f;
if (targetSpeed.sqrMagnitude > 0.0f)
{
var localTargetSpeed = transform.InverseTransformDirection(targetSpeed);
var angleDiff = Vector3.SignedAngle(Vector3.forward, localTargetSpeed.normalized, Vector3.up);
if (angleDiff > 0.0f)
{
rotateSpeed = maxRotateSpeed;
}
else if (angleDiff < 0.0f)
{
rotateSpeed = -maxRotateSpeed;
}
// Assumes that x > NaN is false - otherwise we need to guard against Time.deltaTime being zero.
if (Mathf.Abs(rotateSpeed) > Mathf.Abs(angleDiff) / Time.deltaTime)
{
rotateSpeed = angleDiff / Time.deltaTime;
}
}
// Calculate move delta.
moveDelta = new Vector3(directSpeed.x, moveDelta.y, directSpeed.z);
break;
}
}
Answer by netanelu · Nov 17, 2020 at 03:07 PM
I have just make it work:
Add from the assets store the joystick pack.
on the project view, move the joystick pack folder from assets to asster/LEGO/scripts/LEGO Minifig
the joystick pack folder is with the same folder as the MinifigController c# file.
open minifigController code file, and add public var:
public FixedJoystick joystick;
var targetSpeed = right joystick.Horizontal; targetSpeed += forward joystick.Vertical; within case InputType.Direct:
add a new canvas and add into this canvas an Fixed Joystcik.
select the Pizza Boy and in the inspector select the Minifig Controller and select the joystick to be the joystick you made in section 4.
works for me :)
Your answer
Follow this Question
Related Questions
How to distinguish between two touch screens for input in android 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How do you get position from fingerId 1 Answer
When pressing a button, it counts as pressing the screen and the button [New Input System] 0 Answers
How can we make touch controls like Hole.io/ Bumper.io? 0 Answers