Hello, So when i try to move my character using mobile inputs it lerps inconsistent so when i used keyboard inputs like input get keycode d or a to move my character it worked perfectly but when i switched to using mobile inputs it just doesnt work like when i try to jump its jumping accordingly to swipe force, if it’s faster player jumps faster, if it’s slower player jumps slower etc. i want it to jump at same speed no matter the swipe force and same thing goes for left and right swipes i have script that detects collision and according to that bounces the player back to lane but even that’s working according to swipe force i want it to be the same result no matter the force of the swipe.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Animator robot;
public Rigidbody rb;
public float speed = 5;
public float sidewaySpeed = 10;
public float jumpSpeed = 10;
public float leftTime;
public float centerTime;
public float rightTime;
public float rollTime;
public float range = 2f;
public float hitTime = 0;
public float turnedLeftTime = 0f;
public float turnedRightTime = 0f;
public float minSwipeDistance = 20f;
public Vector3 rightPos;
public Vector3 centerPos;
public Vector3 leftPos;
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
public bool isRight = false;
public bool isLeft = false;
public bool isCenter = true;
public bool roll;
public bool isHit = false;
public bool sideStep = false;
public bool sideStep2 = false;
public bool sideStep3 = false;
public bool sideStep4 = false;
public bool turnedRight = false;
public bool turnedLeft = false;
public bool grounded = false;
public float groundCheckDistance;
public float bufferCheckDistance = 0.1f;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
fingerDownPosition = touch.position;
fingerUpPosition = touch.position;
}
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
{
fingerUpPosition = touch.position;
CheckSwipe();
}
}
if (roll && grounded)
{
rollTime += Time.deltaTime;
robot.Play("rolling");
}
if (rollTime > 0)
{
rollTime += Time.deltaTime;
roll = false;
}
if (rollTime > 0.2)
{
rollTime = 0;
}
if (grounded == false && roll)
{
Physics.gravity = new Vector3(0, -80, 0);
}
if (grounded)
{
Physics.gravity = new Vector3(0, -9.81f, 0);
}
}
void CheckSwipe()
{
if (Vector2.Distance(fingerDownPosition, fingerUpPosition) >= minSwipeDistance)
{
Vector2 swipeDirection = fingerUpPosition - fingerDownPosition;
swipeDirection.Normalize();
if (swipeDirection.y > 0 && swipeDirection.x > -0.5f && swipeDirection.x < 0.5f && grounded == true)
{
GetComponent<Rigidbody>().AddForce(transform.up * jumpSpeed, ForceMode.Impulse);
robot.Play("RobotArmature|Robot_Jump");
// Handle swipe up action
}
else if (swipeDirection.y < 0 && swipeDirection.x > -0.5f && swipeDirection.x < 0.5f && rollTime == 0)
{
roll = true;
// Handle swipe down action
}
else if (swipeDirection.x < 0 && swipeDirection.y > -0.5f && swipeDirection.y < 0.5f && isRight == true && centerTime > 0.23f)
{
isCenter = true;
isRight = false;
isLeft = false;
turnedLeft = true;
robot.Play("RobotArmature_Robot_WalkJump_Left");
// Handle swipe left action
}
else if (sideStep2 == true && isCenter == true)
{
isLeft = true;
isCenter = false;
isRight = false;
sideStep2 = false;
turnedLeft = false;
robot.Play("RobotArmature_Robot_WalkJump_Left");
}
else if (swipeDirection.x < 0 && swipeDirection.y > -0.5f && swipeDirection.y < 0.5f && isCenter == true && leftTime > 0.23f)
{
isCenter = false;
isRight = false;
isLeft = true;
turnedLeft = true;
robot.Play("RobotArmature_Robot_WalkJump_Left");
// Handle swipe left2 action
}
else if (sideStep4 == true && isLeft == true)
{
isCenter = true;
isLeft = false;
isRight = false;
robot.Play("RobotArmature_Robot_WalkJump");
sideStep = false;
}
else if (swipeDirection.x > 0 && swipeDirection.y > -0.5f && swipeDirection.y < 0.5f && isCenter == true && rightTime > 0.23f)
{
isCenter = false;
isRight = true;
isLeft = false;
turnedRight = true;
robot.Play("RobotArmature_Robot_WalkJump");
// Handle swipe right action
}
else if (sideStep3 == true && isRight == true)
{
isCenter = true;
isRight = false;
isLeft = false;
robot.Play("RobotArmature_Robot_WalkJump_Left");
sideStep = false;
}
else if (swipeDirection.x > 0 && swipeDirection.y > -0.5f && swipeDirection.y < 0.5f && isLeft == true && centerTime > 0.23f)
{
isCenter = true;
turnedRight = true;
isLeft = false;
isRight = false;
robot.Play("RobotArmature_Robot_WalkJump");
// Handle swipe right2 action
}
else if (sideStep == true && isCenter == true&& turnedRightTime == 0)
{
isLeft = true;
isCenter = false;
isRight = false;
sideStep = false;
turnedRight = false;
}
}
}
void FixedUpdate()
{
groundCheckDistance = 0.3f;
speed += Time.fixedDeltaTime / 20;
if (isCenter == true)
{
turnedLeftTime += Time.deltaTime;
turnedRightTime += Time.deltaTime;
sideStep = false;
sideStep2 = false;
sideStep3 = false;
sideStep4 = false;
}
if (isLeft)
{
turnedLeftTime = 0;
turnedRightTime = 0;
}
if (isRight)
{
turnedRightTime = 0;
turnedLeftTime = 0;
}
if (turnedLeftTime > 0.23f)
{
turnedLeft = false;
turnedLeftTime = 0;
}
if (turnedRightTime > 0.23f)
{
turnedRight = false;
turnedRightTime = 0;
}
Vector3 directionRay1 = new Vector3(0, 2, 3);
Ray theRay = new Ray(transform.position, transform.TransformDirection(directionRay1 * range));
Debug.DrawRay(transform.position, transform.TransformDirection(directionRay1 * range));
if (Physics.Raycast(theRay, out RaycastHit hitd, range))
{
if (hitd.collider.CompareTag("obstacle"))
{
robot.Play("RobotArmature_Robot_Death");
speed = 0;
}
}
Vector3 directionRay2 = new Vector3(2, 2, 0);
Ray theRay2 = new Ray(transform.position, transform.TransformDirection(directionRay2 * range));
Debug.DrawRay(transform.position, transform.TransformDirection(directionRay2 * range));
if (Physics.Raycast(theRay2, out RaycastHit hitd2, range))
{
if (hitd2.collider.CompareTag("obstacle") && turnedRight == true)
{
sideStep = true;
}
if (hitd2.collider.CompareTag("obstacle") && isRight == true)
{
sideStep3 = true;
}
else
{
sideStep3 = false;
}
}
Vector3 directionRay3 = new Vector3(-2, 2, 0);
Ray theRay3 = new Ray(transform.position, transform.TransformDirection(directionRay3 * range));
Debug.DrawRay(transform.position, transform.TransformDirection(directionRay3 * range));
if (Physics.Raycast(theRay3, out RaycastHit hitd3, range))
{
if (hitd3.collider.CompareTag("obstacle") && turnedLeft == true)
{
sideStep2 = true;
}
if (hitd3.collider.CompareTag("obstacle") && isLeft == true)
{
sideStep4 = true;
}
else
{
sideStep4 = false;
}
}
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, groundCheckDistance))
{
grounded = true;
}
else
{
grounded = false;
}
if (grounded == false)
{
robot.SetBool("grounded", false);
}
if (grounded == true)
{
robot.SetBool("grounded", true);
robot.SetBool("isJump", false);
}
transform.Translate(0, 0, speed * Time.fixedDeltaTime);
Vector3 pos = transform.position;
pos.x = rightPos.x;
if (isRight == true)
{
pos.x = rightPos.x;
transform.position = Vector3.Lerp(transform.position, pos, sidewaySpeed * Time.deltaTime);
centerTime += Time.fixedDeltaTime;
rightTime = 0;
leftTime = 0;
}
if (isCenter == true)
{
pos.x = centerPos.x;
transform.position = Vector3.Lerp(transform.position, pos, sidewaySpeed * Time.deltaTime);
leftTime += Time.fixedDeltaTime;
rightTime += Time.fixedDeltaTime;
centerTime = 0;
}
if (isLeft == true)
{
pos.x = leftPos.x;
transform.position = Vector3.Lerp(transform.position, pos, sidewaySpeed * Time.deltaTime);
leftTime = 0;
rightTime = 0;
centerTime += Time.fixedDeltaTime;
}
}
}
any help is appreciated.