P
P
Ptichka0072021-11-30 18:58:24
Unity
Ptichka007, 2021-11-30 18:58:24

The object is not moving, what should I do?

It is necessary to create a system of N objects that move like runners in a relay race: only one runs until it reaches the other. As soon as the distance to the next "runner" is 0, the object ceases to be a "runner", it becomes the next object. And so in a circle.
Here is my code:

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

public class Ex2 : MonoBehaviour
{
    public int n;
    public int i;
    public GameObject[] runners;
    public Transform[] points_runners;
    public Vector3 target;
    
    public GameObject runner;
    void Start()
    {
        i = 0;
        n = i + 1;
        runner = runners[i];
        target = points_runners[n].position;
        runner.transform.LookAt(target);
        
    }


    void Update()
    {
       if (runner.transform.position == target)
        {
            runner = runners[Get_runner()];
            target = points_runners[Get_Aim()].position;
            runner.transform.LookAt(target);

        }

        Move_to_next();


    }

    public void Move_to_next()
    {
        runner.transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * 2f);
    }

    public int Get_Aim()
    {

        n = n + 1;
        return n;
    }
    public int Get_runner()
    {
        i = i + 1;
        
        return i;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ente, 2021-11-30
@Ptichka007

Do it all on triggers, make one Runner.cs script
1) hang a SphereCollider on each participant.
2) check the IsTrigger box on it.
3) add a rigidbody to it and make it kinematic.
4) in the target field, enter its goal, the transform to which it should run.
5) check the isReadyToRun checkbox in the inspector for the first runner.

[SerializedField] private Transform target;
[SerializedField] private bool isReadyToRun;

private void OnTriggerEnter()
{
         isReadyToRun = !isReadyToRun;
}

private void Update()
{
      if (!isReadyToRun) return;
      transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * 2f);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question