@ketsymi1

Почему персонаж не двигается и не показывает анимации?

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


public class mouse : MonoBehaviour
{
    public float speed;
    public float sensetiv;
    float horiz;
    float vert;
    public GameObject Player;
    float x;
    float z;
    private Animator PlayerAnimator;



    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        PlayerAnimator = Player.GetComponent<Animator>();
        
    }


    void Update()
    {
        x = Input.GetAxis("Horizontal") * speed;
        z = Input.GetAxis("Vertical") * speed;
        horiz += Input.GetAxis("Mouse X") * sensetiv;
        vert += Input.GetAxis("Mouse Y") * sensetiv;
        vert = Mathf.Clamp(vert, -20, 20);
        Player.transform.Translate(x, 0, z);
        transform.localRotation = Quaternion.Euler(-vert, 0, 0);
        Player.transform.localRotation = Quaternion.Euler(0, horiz, 0);
        PlayerController();
    }



void PlayerController()
{
    if (z > 0)
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            PlayerAnimator.SetInteger("Move", 2);
            speed = 4;
        }
        else
        {
            PlayerAnimator.SetInteger("Move", 1);
            speed = 2;
        }
    }
    else if (z < 0)
    {
        PlayerAnimator.SetInteger("Move", -1);
        speed = 2;
    }
    else
    {
        PlayerAnimator.SetInteger("Move", 0);
        speed = 0;
    }
}
}

Код поставлен на камеру
Персонаж может вращаться,а ходить нет
  • Вопрос задан
  • 51 просмотр
Пригласить эксперта
Ответы на вопрос 1
Lapita12
@Lapita12
Тесты, тесты?
1. Добавить Rigidbody к персонажу
2. Обновить скрипт для работы с Rigidbody

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

public class Mouse : MonoBehaviour 
{
    public float speed;
    public float sensitive;
    
    float horizontal;
    float vertical;
    float x;
    float z;
    
    public GameObject player;
    
    private Animator playerAnimator;
    private Rigidbody playerRigidbody;
    
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        playerAnimator = player.GetComponent<Animator>();
        playerRigidbody = player.GetComponent<Rigidbody>();
    }

    void Update() 
    {
        x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        z = Input.GetAxis("Vertical") * speed * Time.deltaTime;

        horizontal += Input.GetAxis("Mouse X") * sensitive;
        vertical += Input.GetAxis("Mouse Y") * sensitive;
        vertical = Mathf.Clamp(vertical, -20, 20);

        Vector3 newPosition = playerRigidbody.position + player.transform.right * x + player.transform.forward * z;
        playerRigidbody.MovePosition(newPosition);

        transform.localRotation = Quaternion.Euler(-vertical, 0, 0);
        player.transform.localRotation = Quaternion.Euler(0, horizontal, 0);
        
        PlayerController(); 
    }

    void PlayerController() 
    {
        if (z > 0) 
        {
            if (Input.GetKey(KeyCode.LeftShift)) 
            {
                playerAnimator.SetInteger("Move", 2);
                speed = 4;
            } 
            else 
            {
                playerAnimator.SetInteger("Move", 1);
                speed = 2;
            }
        } 
        else if (z < 0) 
        {
            playerAnimator.SetInteger("Move", -1);
            speed = 2;
        } 
        else 
        {
            playerAnimator.SetInteger("Move", 0);
            speed = 0;
        }
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы