Hi! I’m not that new to programming but for Unity, I’m a newbie.
I’m trying to create a TPS mobile game, but I can’t achieve to the player move where the camera facing.
This is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputHandler : MonoBehaviour {
public Animator animator;
private float speedPercentChanged;
public Transform playerTransform;
public FixedJoystick Joystick1;
public Vector2 moveJoystick;
void Update () {
float speedPercent;
moveJoystick = Joystick1.inputVector;
if (Mathf.Abs(moveJoystick.y) < Mathf.Abs(moveJoystick.x))
speedPercent = Mathf.Abs(moveJoystick.x);
else
speedPercent = Mathf.Abs(moveJoystick.y);
animator.SetFloat("speedPercent", speedPercent);
if (moveJoystick.x != 0 && moveJoystick.y != 0)
{
Vector3 movement = new Vector3(moveJoystick.x, 0.0f, moveJoystick.y);
playerTransform.rotation = Quaternion.LookRotation(movement);
playerTransform.Translate(movement * (5 * speedPercent) * Time.deltaTime, Space.World);
}
}
}
I can’t find any posts that has the solution for this. (All the post used something else than transform.Translate)