T
T
Timur9757872021-12-03 20:12:36
C++ / C#
Timur975787, 2021-12-03 20:12:36

The script does not work, what's the problem?

I have a script, this script is responsible for the player's walking. There was a problem, the script partially does not work. When touched, the script should display the text and move the player, but it only displays the text. Here is the script itself:

using UnityEngine;
using UnityEngine.UI;

public class Walk : MonoBehaviour
{
    private Animator player;
    public float speddZ;
    private Rigidbody rb;
    public Finich fenich;
    private float deltaPosX;
    public Text text;

    void Start()
    {
        player = gameObject.GetComponent<Animator>();
        rb = gameObject.GetComponent<Rigidbody>();
    }
    void Update()
    {
      if (fenich.win == false) 
      {
           
            if (Input.touchCount == 1)
            {
                text.text = "Touch";
                deltaPosX = Input.GetTouch(1).deltaPosition.x;
                rb.velocity = new Vector3(-deltaPosX * Time.deltaTime * 100, 0, speddZ * Time.deltaTime);
                player.Play("Walking");
            }
            else 
            { 
            rb.velocity = new Vector3(0, 0, speddZ * Time.deltaTime);
            player.Play("Walking");
      }     }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-12-03
@vabka

You have these lines in your code:

rb.velocity = new Vector2(-deltaPosX * Time.deltaTime * 100, 0);
rb.velocity = new Vector3(0, 0, speddZ * Time.deltaTime);

So the problem may be in them:
1. You change the speed twice within one frame - in fact, the first line will not even work out
. And the second line will lead the character along the forward / backward (z) axis, which is quite strange for a 2d game.
2. If you do not move your finger, then the speed will be 0, because you take deltaPosition
3. For some reason, the speed is tied to the frame time. The higher the fps, the slower the character will move.
(maybe instead of speed you wanted to change acceleration / momentum?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question