D
D
DANICH702021-01-05 11:38:16
Unity
DANICH70, 2021-01-05 11:38:16

I don't understand what is the problem with GameObject (Unity 3D)?

(UNITY 3D) In ​​general, I have a platform and a Game Object in my game. I made it so that the Game Object always moves straight and is controlled by the joystick in X. I also made it so that the camera always moves straight (but without joystick control :). That is, everyone has the same speed, and thus the camera follows the GameObject. But! For some reason, after a couple of seconds (4 seconds), the GameObject stops. I don't understand what the problem is. Here is the code on the GameObject:

using UnityEngine;
using System.Collections;
public class NewBehaviour : MonoBehaviour
{
    public GameObject player;
    public int speedRotation = 3;
    public int speed = 5;
    public int jumpSpeed = 50;

    void Start()
    {
        player = (GameObject)this.gameObject;
    }
    void Update()
    {

        {
            player.transform.position += player.transform.forward * speed * Time.deltaTime;
        }
    }
}


And here it is on camera:
using UnityEngine;
using System.Collections;
public class camera : MonoBehaviour
{
    public GameObject player;
    public int speedRotation = 3;
    public int speed = 5;
    public int jumpSpeed = 50;

    void Start()
    {
        player = (GameObject)this.gameObject;
    }
    void Update()
    {

        {
            player.transform.position += player.transform.up * speed * Time.deltaTime;
        }
    }
}


And on the joystick:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyScript : MonoBehaviour
{


    protected Joystick joystick;


    void Start()
    {
        joystick = FindObjectOfType<Joystick>();
    }

    // Update is called once per frame
    void Update()
    {
        var rigidbody = GetComponent<Rigidbody>();

        rigidbody.velocity = new Vector3(joystick.Horizontal * 10f,
                                         rigidbody.velocity.y);

    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question