- Home /
How to change movement script for computer for android.
Hello, i have this script but it is made for a computer not android. i am trying to make this game for android i am stuck on the movement. if you can make it into an android script i already have a joystick pack press here to see. Here is the script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(CharacterController))]
public class Movement : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public VariableJoystick variableJoystick;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
}
void Update()
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
Thank you so much but do you know how to change the rotation bit to be a finger instead of a mouse
Yes I have done something similar but a bit complicated. You'll need to use Input.GetTouch()
to get the positions of multiple touches on the screen simultaneously. I would use Input.GetTouch(1) for now, so the second touch on the screen is the mouse movement, and the first is the joystick. Anyway, here's the full script (i'm not entirely sure if it works):
public class LookAround : MonoBehaviour
{
Vector2 AnchorPoint;
Vector3 PreviousCameraRotation;
public float LookSensitivity;
private void Start()
{
AnchorPoint = new Vector2(Screen.width / 2, Screen.height / 2);
PreviousCameraRotation = transform.rotation.eulerAngles;
}
void Update()
{
CameraLook();
}
private void CameraLook()
{
if (Input.touchCount < 1)
return;
Vector2 touchPos = Input.GetTouch(1).position;
int ScreenWidth = Screen.currentResolution.width;
int ScreenHeight = Screen.currentResolution.height;
float xRotation = (touchPos.y - AnchorPoint.y) / ScreenHeight * LookSensitivity;
float yRotation = (touchPos.x - AnchorPoint.x) / ScreenWidth * LookSensitivity;
transform.rotation = Quaternion.Euler
(
-xRotation + PreviousCameraRotation.x,
yRotation + PreviousCameraRotation.y,
transform.rotation.z
);
}
}
Hello i just tested it and it does not work if you have any other scripts like this could you please say. Thanks @Llama_w_2Ls
One more thing, can i make the jump key into a button instead of space @Llama_w_2Ls
You would need a bool called IsJumping, and a public function that is assigned to the button's on click event. For example:
bool IsJumping = false;
//....
void Update()
{
if (IsJumping)
{
//...
IsJumping = false;
}
}
// This is assigned to the button's on click event in the inspector
public void Jump()
{
IsJumping = true;
}
@Bentley100 please accept the answer as correct if it helped enough. Thanks
Answer by Llama_w_2Ls · Mar 29, 2021 at 06:57 PM
The link doesn't work, but if you're talking about the free Fenerax joystick pack on the asset store Fenerax joystick package
Then you can replace Input.GetAxis("Horizontal")
with joystick.horizontal
, for example. Here's some general code:
public VariableJoystick joystick;
void Update()
{
// Same as Input.GetAxis() for horizontal/vertical asx
float x = joystick.horizontal;
float z = joystick.vertical;
// Movement code...
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613031449im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Everything is choppy... How can I make object movement more fluid? 3 Answers
2D Vector Movement,Vectorel Movement with Buttons 2 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
Android APK acts different than in Editor 1 Answer
Unity Android movement 1 Answer