- Home /
Question by
_Amine_ · Aug 05, 2018 at 04:15 PM ·
movementcharactercontrollerplayer movement
i'm trying to move the player horizontal and vertical in lanes but he keep shaking/vibrating
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePlayer : MonoBehaviour {
public static MovePlayer instance;
[SerializeField] float speed;
[SerializeField] float laneDistance;
public int lane = 1;
public int altd = 1;
[SerializeField] CharacterController controller;
void Awake()
{
if (instance == null) { instance = this; }
}
void Start()
{
}
void Update()
{
Debug.Log("Lane " + lane);
Debug.Log("vert " + altd);
if (!GamePlayManager.instance.gameStarted || GamePlayManager.instance.gameOver)
{
return;
}
if (MobileInput.instance.SwipeLeft)
{
MoveLane(false);
}
if (MobileInput.instance.SwipeRight)
{
MoveLane(true);
}
if (MobileInput.instance.SwipeUp)
{
MoveAlt(true);
}
if (MobileInput.instance.SwipeDown)
{
MoveAlt(false);
}
Vector3 targetPos = transform.position.z * Vector3.forward;
if (lane == 0)
{
targetPos += Vector3.left * laneDistance;
}
else if (lane == 2)
{
targetPos += Vector3.right * laneDistance;
}
if (altd == 0)
{
targetPos += Vector3.down * laneDistance;
}
else if (altd == 2)
{
targetPos += Vector3.up * laneDistance;
}
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPos - transform.position).normalized.x * speed;
moveVector.y = (targetPos - transform.position).normalized.y * speed;
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
void MoveLane(bool gRight)
{
lane += (gRight) ? 1 : -1;
lane = Mathf.Clamp(lane, 0, 2);
}
void MoveAlt(bool gUp)
{
altd += (gUp) ? 1 : -1;
altd = Mathf.Clamp(altd, 0, 2);
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "Obstacle")
{
GamePlayManager.instance.gameOver = true;
Debug.Log("GameOver");
}
}
}
,
Comment