При использовании метода
Move
у компонента
CharacterController
При смене кадра позиция по оси
Z
приравнивается к 0, вопрос в том как приравнивать её к значению переменной
newPos
?
Код C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
private CharacterController _characterController;
private Vector3 moveVec;
public float speed;
private int laneNumber = 1,
lanesCount = 2;
public float firstsLanePos,
laneDistance,
sideSpeed;
private bool didChangeLastFrame = false;
void Start()
{
_characterController = GetComponent<CharacterController>();
moveVec = new Vector3(1, 0);
}
void Update()
{
moveVec.x = speed;
moveVec *= Time.deltaTime;
float input = Input.GetAxis("Horizontal");
if (Mathf.Abs(input) > .1f)
{
if (!didChangeLastFrame)
{
didChangeLastFrame = true;
laneNumber += (int)Mathf.Sign(input);
laneNumber = Mathf.Clamp(laneNumber, 0, lanesCount);
Debug.Log("[LaneNumber] = " + laneNumber);
}
}
else
{
didChangeLastFrame = false;
}
Vector3 newPos = transform.position;
newPos.z = Mathf.Lerp(newPos.z, firstsLanePos + (laneNumber * laneDistance), Time.deltaTime * sideSpeed);
Debug.Log("[NewPos.z] = " + newPos.z);
transform.position = newPos;
_characterController.Move(moveVec);
}
}