Answer the question
In order to leave comments, you need to log in
Does changing the position of the player along the Z axis stop working?
When using the method Move
on the component CharacterController
When changing the frame, the position along the axis is Z
equal to 0, the question is how to equate it to the value of the variable newPos
?
C# code:
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);
}
}
Answer the question
In order to leave comments, you need to log in
Why do you have "moveVec = new Vector3(1, 0);" if it's a Vector3?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question