Problem ze skryptem

Programowanie w Unity.

Problem ze skryptem

Postprzez Kasprovskyy » 18 Mar 2022, 22:14

Cześć, posieadam problem z "moim" skryptem. Gdy odpalam "grę" moja postać idzie jakoś dziwnie do przodu (kilka jednostek na sekundę). Wie ktoś gdzie jest problem?


Kod: Zaznacz wszystko
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playermovement : MonoBehaviour
{
    public Transform trans;
    public Transform modelTrans;
    public CharacterController characterController;
    public float movespeed = 24;
    public float timeToMaxSpeed = .26f;
    private float VelocityGainPerSecond { get { return movespeed / timeToMaxSpeed; } }
    public float timeToLoseMaxSpeed = .2f;
    private float VelocityLossPerSecond { get { return movespeed / timeToLoseMaxSpeed; } }
    public float reverseMomentumMultiplier = 2.2f;
    private Vector3 movementVelocity = Vector3.zero;

    void Move()
    {
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            if (movementVelocity.z >= 0)
                movementVelocity.z = Mathf.Min(movespeed, movementVelocity.z + VelocityGainPerSecond * Time.deltaTime);
            else
                movementVelocity.z = Mathf.Min(0, movementVelocity.z + VelocityGainPerSecond * reverseMomentumMultiplier * Time.deltaTime);

        }

        else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            if (movementVelocity.z > 0)
                movementVelocity.z = Mathf.Max(0, movementVelocity.z - VelocityGainPerSecond * reverseMomentumMultiplier * Time.deltaTime);
            else
                movementVelocity.z = Mathf.Max(-movespeed, movementVelocity.z - VelocityGainPerSecond * Time.deltaTime);

        }

        else
        {
            if (movementVelocity.z > 0)
                movementVelocity.z = Mathf.Max(0, movementVelocity.z - VelocityLossPerSecond * Time.deltaTime);

            else
                movementVelocity.z = Mathf.Min(0, movementVelocity.z + VelocityLossPerSecond * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            if (movementVelocity.x >= 0)
                movementVelocity.x = Mathf.Min(movespeed, movementVelocity.x + VelocityGainPerSecond * Time.deltaTime);
            else
                movementVelocity.x = Mathf.Min(0, movementVelocity.x + VelocityGainPerSecond * reverseMomentumMultiplier * Time.deltaTime);

        }

        else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            if (movementVelocity.x > 0)
                movementVelocity.x = Mathf.Min(0, movementVelocity.x - VelocityGainPerSecond * reverseMomentumMultiplier * Time.deltaTime);
            else
                movementVelocity.x = Mathf.Min(-movespeed, movementVelocity.x + VelocityGainPerSecond * Time.deltaTime);

        }

        else
        {
            if (movementVelocity.x > 0)
                movementVelocity.x = Mathf.Max(0, movementVelocity.x - VelocityLossPerSecond * Time.deltaTime);

            else
                movementVelocity.x = Mathf.Min(0, movementVelocity.x + VelocityLossPerSecond * Time.deltaTime);
        }

        if (movementVelocity.x != 0 || movementVelocity.z != 0)
            characterController.Move(movementVelocity * Time.deltaTime);
            modelTrans.rotation = Quaternion.Slerp(modelTrans.rotation, Quaternion.LookRotation(movementVelocity), .18F);
    }

   
    // Update is called once per frame
    void Update()
    {
        Move();
    }
}
Kasprovskyy
 
Posty: 1
Rejestracja: 06 Mar 2022, 20:22
Has thanked: 0 time
Been thanked: 0 time

Wróć do Skrypty

Kto jest na forum

Użytkownicy przeglądający to forum: Bing [Bot], Google [Bot] oraz 6 gości