D
D
dark_spectator2019-02-14 00:23:54
C++ / C#
dark_spectator, 2019-02-14 00:23:54

Does changing the position of the player along the Z axis stop working?

When using the method Moveon the component CharacterController
When changing the frame, the position along the axis is Zequal 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

1 answer(s)
U
UnityMakar, 2019-02-15
@UnityMakar

Why do you have "moveVec = new Vector3(1, 0);" if it's a Vector3?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question