A
A
alex4answ2019-07-12 06:53:44
Unity
alex4answ, 2019-07-12 06:53:44

How to stop the movement of an object on a checkerboard when it reaches the edge of the field?

Good afternoon, roughly speaking, there is a chessboard, you need to move the object from one edge to the other (if there is an obstacle along the way, we slow down the object).
And so, it seems the task is simple, we move:

float speed = 30f;
bool isMove = false;
 
void Upload(){
    if(isMove) // true когда мы нажали кнопку "вперед"
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
}

Everything is fine, it is moving, but the problem itself is to stop the object if there is an obstacle or the edge of the map.
I did this: I
made colliders along the edges of the map, gave them tag = obstacles (obstacle), actually did the same with obstacles, made them isTrigger, by the way, the game object itself does not need physics, because it isKinematic = true.
Stop the player:
void OnTriggerEnter(Collider collider)
    {
        if (collider.tag == "Obstacles")
        {
            Debug.Log("STOP");
            isMove = false;
        }
    }

But due to the high speed of movement (and it is needed), OnTriggerEnter fires when the game object has already taken off from the field.
Unity docs say that OnTriggerEnter doesn't guarantee collision, ok, let's make player isKinematic = false, remove isTrigger from obstacles.
Change OnTriggerEnter to OnCollisionEnter and tweak it a bit, Voilà, it works, but if it's still not right.
Problems:
1. It still sometimes "jumps" and works not at the beginning of the collision, but when the object has already passed the obstacle halfway.
2. if I rotate the object, I can’t start moving because the colliders are in contact (as an option, when stopping, move the player back a little, but it’s a crutch)
Question - How to make the player slow down so that he does not get stuck in the textures and does not bug, so that he walks "exactly" through the cells?
PS The game is in 3d, the camera is on top of this "mini game", then my "chess" will rotate, in general, everything is in 3d, picture:
5d280443cc1bc285823362.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2019-07-12
@alex4answ

Killer auto-chess checkers? Your problem is that you are mixing logic with animation. Chess is a strict discrete single-valued matrix of objects with complete information.
1. Create a matrix 2. Fill the matrix with figures 3. When moving, a new place is stupidly calculated by cells 4. Start the animation "move from point A to point B in time T" MyGameObject[,] =new MyGameObject[4,5]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question