- Home /
simple character movement
Hello,
I need help with the script I found on the internet. It works OK on PC but I need it for mobile and I can't get it working with unity premade buttons (cross platform input). Could you help me how could I control the character using mobile buttons? Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float rotateSpeed = 3.0F;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
//Rotate Player
transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
}
}
Also there is a small problem when reversing - the cube is reversing wrong side. If you know how to fix it please let me know.
Thank you so much!
what do you mean byu mobile inputs? buttons in the canvas?
@xxmariofer yes, normal buttons in canvas so I could control the cube on mobile
create 2 int vars that will be set to 1 when the button is clicked and to 0 when is released, just change those vars for the input.getaxis and a third var for the getbutton jump and use a method similar to this
public void Horizontal$$anonymous$$ovement(int direction){ horizontalDirection = direction; }
public void Horizontal$$anonymous$$ovementRelease(){ horizontalDirection = 0; }
just change the horizontaldirection value for the axis, and assign those method to the buttons.
@xxmariofer I am so sorry but I am beginner with scripting in C sharp. Could u please post it in complete script? Thanks for your time.
Your answer
Follow this Question
Related Questions
character(ball) rolls with less speed when I build it 2 Answers
How does character selection in endless runner works? 0 Answers
How can I make my character run over a cube? 0 Answers
Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer
How to move a character from the middle of one box to the other? 2 Answers