I found a lesson where the smooth movement of physical objects around the map is done using buttons. I decided to make it possible to move only with the mouse (that is, where the cursor is looking, there is an object). But when I aim at an object, it flies to the side of the cursor. Rigidbody and BoxCollider stand at the facility itself. Cave is the ground on which the object moves.
using UnityEngine; //эта строчка гарантирует что наш скрипт не завалится ести на плеере будет отсутствовать компонент Rigidbody [RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { // т.к. логика движения изменилась мы выставили меньшее и более стандартное значение public float Speed = 1f; //что бы эта переменная работала добавьте тэг "Ground" на вашу поверхность земли float raycastLeight = 100; private Rigidbody _rb; RaycastHit _hit; private Vector3 movement; void Start() { _rb = GetComponent<Rigidbody>(); } void FixedUpdate() { if (GlobalVar.activeCreateUnit == true) { GameObject _target = GameObject.FindWithTag("CreateUnit"); Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(_ray, out _hit, raycastLeight)) { if ((_hit.collider.name == "Cave") && (_target != null)) { // в даном случае допустимо использовать это здесь, но можно и в Update. // но раз уж вызываем здесь, то // двигать будем используя множитель fixedDeltaTimе MovementLogic(); } if ((GlobalVar.onTriggerUnit == false)&&(Input.GetMouseButton(0))) { GlobalVar.activeCreateUnit = false; } if(Input.GetMouseButton(1)) { Destroy(_target); } } } } private void MovementLogic() { Vector3 mousePosition = new Vector3(Input.mousePosition.x, 0, Input.mousePosition.y); movement = Camera.main.ScreenToWorldPoint(mousePosition); movement = new Vector3(movement.x, 0, movement.z); // что бы скорость была стабильной в любом случае // и учитывая что мы вызываем из FixedUpdate мы умножаем на fixedDeltaTimе transform.Translate(movement * Speed * Time.fixedDeltaTime); } }
IsGroundedwas better for which it would be worth paying attention to. - Andrew