Search code examples
c#unity-game-enginegame-development

How to keep Horizontal Movement Value thru time in c# Unity?


So I am making game and I need good PlayerMovement Script but there is little problem. You see I made so that my player is following this path (I didn't make it I downloaded it from marketplace) and he can go only forward. Then I put another Vector3 (horizontalMove) value and hooked it to transform.position and didn't get what I really wanted. When I hit play everything works and I can go Left to Right but my position does not stay the same. The moment I let go of my keys (a, d) my player returns to track, resets in middle. I need it to stay in the position I left him. And too, just adding rb.position in my transform.position just send me flying in the air. I need help.

`public class PathFollower : MonoBehaviour
{
    public PathCreator pathCreator;
    public EndOfPathInstruction endOfPathInstruction;
    public float speed = 5;
    float distanceTravelled;

    float horizontalInput;
    [SerializeField] float horizontalMultiplier = 2;
    public Rigidbody rb;

    void Start() {
        if (pathCreator != null)
        {
            // Subscribed to the pathUpdated event so that we're notified if the path changes during the game
            pathCreator.pathUpdated += OnPathChanged;
        }
    }

    void Update()
    {
        if (pathCreator != null)
        {
            horizontalInput = Input.GetAxis("Horizontal");
            Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
            distanceTravelled += speed * Time.deltaTime;
            Debug.Log();
            transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction) + new Vector3 (0,1,0) + horizontalMove;
            transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
        }
    }

    // If the path changes during the game, update the distance travelled so that the follower's position on the new path
    // is as close as possible to its position on the old path
    void OnPathChanged() {
        distanceTravelled = pathCreator.path.GetClosestDistanceAlongPath(transform.position);
    }
}
}`

Solution

  • You need a global variable that holds the value of horizontalMove, so it isn't reset in the update loop.

    Vector3 horizontalMove;
    void Update()
        {
            if (pathCreator != null)
            {
                horizontalInput = Input.GetAxis("Horizontal");
                Vector3 horizontalMove += transform.right * horizontalInput * speed * Time.deltaTime * horizontalMultiplier;
                distanceTravelled += speed * Time.deltaTime;
                Debug.Log();
                transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction) + new Vector3 (0,1,0) + horizontalMove;
                transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
            }
        }
    

    I also took the liberty to change fixedDeltaTime to deltaTime, since that is the appropriate variable for an Update context.