N
N
Nikita Salnikov2020-02-19 13:10:41
C++ / C#
Nikita Salnikov, 2020-02-19 13:10:41

Why doesn't character movement correspond to keyboard methods?

Hello. Again there was a question on the code from the book. Why character movement through the keyboard does not match. I wrote the code as in the book, I checked it, there are no errors. but the character, when starting the game and pressing the keys, moves in other directions: A - moves back, D - forward, W - left, S - right. Accordingly, the arrows also. I will be very grateful for your help.

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

[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPS Input")]

public class FPSInput : MonoBehaviour
{
    public float speed = 6.0f;

    public float gravity = -9.8f;

    private CharacterController _charController;

    // Start is called before the first frame update
    void Start()
    {
        _charController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float deltaX = Input.GetAxis("Horizontal") * speed;
        float deltaZ = Input.GetAxis("Vertical") * speed;

     
        Vector3 movement = new Vector3(deltaX, 0, deltaZ);
        movement = Vector3.ClampMagnitude(movement, speed); 

        movement.y = gravity;

        movement *= Time.deltaTime;
        movement = transform.TransformDirection(movement); 
        _charController.Move(movement);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-02-19
@nikitasalnikov

Try swapping deltaX and deltaZ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question