Q
Q
Qerriom Magnelius2022-04-20 21:45:12
C++ / C#
Qerriom Magnelius, 2022-04-20 21:45:12

How to make control in Unity a character (error in the code)?

Hello! There was such a problem.
I am doing a project to test my capabilities, but I ran into a problem! Everything works in the code, but the problem is with the movement itself

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


public class forward : MonoBehaviour
{
    public float speed = 100000;
    GameObject Player;
    


    // Start is called before the first frame update
    void Start()
    {
        Debug.Log ("forward");
        Player = (GameObject)this.gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        
        
        
        
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log ("forward");
            
            Forward();
        }
      
    }

    

    void Forward()
    {
        Player.transform.position += Player.transform.forward * speed * Time.deltaTime;
    }
}


The code is bound to the button
The most surprising thing is that the part of the code where the button is clicked ( if (Input.GetMouseButtonDown(0))) works, and Debug.Log () outputs forward to the console.
Help me pls(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mimocodil, 2022-04-20
@MicroProger

Good evening. Replace

Player.transform.position += Player.transform.forward * speed * Time.deltaTime;

On the
transform.position += transform.forward * speed * Time.deltaTime;

And change the speed value, at least to
public float speed = 50;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question