R
R
royallion2022-01-24 20:48:14
Unity
royallion, 2022-01-24 20:48:14

Why is the movement script not working?

I want to create a 3D game with a top view, I wrote a script with character movement, but it does not work.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 dir;
    [Serializefield] private int speed;

    private int lineToMove = 5;
    public float lineDistance = 4;
    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }
    private void Update()
    {
        if (SwipeController.swipeRight)
        {
            if (lineToMove < 2)
                lineToMove++;
        }


        if (SwipeController.swipeleft)
        {
            if (lineToMove > 0)
                lineToMove--;
        }

        Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
        if (lineToMove == 0)
            targetPosition += Vector3.left * lineDistance;
        else if (lineToMove == 2)
            targetPosition += Vector3.right * lineDistance;

        transform.position = targetPosition;
    }



    // Update is called once per frame
    void FixedUpdate()
    {
        dir.z = speed;
CharacterController controller = this.controller;
        controller.Move(dir * Time.fixedDeltaTime);
    }
}

internal class dir
{
    public static int z { get; internal set; }
}

internal class SerializefieldAttribute : Attribute
{
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question