A
A
Alex Blinov2019-07-11 15:57:56
C++ / C#
Alex Blinov, 2019-07-11 15:57:56

How to fix the code so that the object is only directed in a straight line, without "drifting"?

Hello, here is the code:

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

public class RotateTank2 : MonoBehaviour
{


    public Transform Selftransform;

    private Vector3 _force;


    private int rotate = 1;






    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {

        Selftransform.position += _force;

        if (rotate == 1)
        {
            transform.Rotate(0, 0, 2);

        }

        if (Input.GetKey(KeyCode.D))
        {
            rotate = 0;
            _force += (Selftransform.up * Time.deltaTime) * 0.1f;


        }
        else
        {
            _force = Vector3.Lerp(_force, Vector3.zero, 10);
            rotate = 1;
        }

    }
}


By pressing the button, the tank moves in a straight line, but when it collides with another object, it moves along the vector in the same way in a straight line, but sideways. How to do so. so that the object moves only in a straight line, regardless of the influence of another force on it (that is, how to repeat the forward movement of a real tank).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2019-07-12
@CEPII

1. Turn off collision physics and move through transform
2. or use a perfectly elastic material and turn off roll and roll inertia.

S
sulim000333, 2019-07-16
@sulim000333

There is a "look at" function - lookAt(target.tranform.position); change the rotation so that the blue arrow points forward of the tank and apply this function, it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question