There is an object that must move in order at different angles of the screen in order, this part is implemented:

transform.position = Vector3.MoveTowards(transform.position, _targetPosition, player.GetSpeed()); 

_targetPosition is one of four vectors. the object must move to a point with these coordinates. The problem arises in writing the following function - when a point is reached, some kind of trigger for a collision with the borders of the screen should be triggered, and the object will fly off with the speed damping towards the center of the camera. Earlier in this place was a crutch.

I read about rigidbody.AddForce and transform.Translate both methods look like something similar to what I need, and how to combine is not enough imagination.

    1 answer 1

    If you want to use physics then you need a Rigidbody.Addforce + Collider on your object. Then you place colliders on the borders of the screen as a fence and the physics themselves will work out a rebound for you, only you need to adjust the parameters. True, if it is tied to physics, then practice shows that there will be difficulties in order to dock the rest of the code where physics is not used, for example, to drastically change the speed of the object, etc. In order to fix the fact of a blow, you can use OnCollisionEnter , OnCollisionExit and OnCollisionStay (the start of the collision, the end of the collision and in the collision process, respectively) for processing. To do this, add these methods to any component on the object where Collider is present.

    If you need to handle your own algorithm in a collision, then you can set the Trigger checkbox in the collider, then there will be no rebound, but the fact of touching the two colliders can be caught through the OnTriggerEnter , OnTriggerExit , OnTriggerStay . Or as an option to make a working area in the form of a large trigger and check that your object is inside through OnTriggerStay , otherwise return it to the comfort zone)

    Well, the most difficult way is to calculate the coordinates of the screen, at each frame to check that the object with some size goes beyond the limits of the active area and try to steer itself by the process of its movement.

    • I tried AddForce, but I need static speed when the object is moving, plus the object should only move from corner to corner, this approach does not seem to suit me very much. There is also such a moment that an empty object hangs on the object, which works as a target for the camera, for its inclination depending on the position of the object, which causes the camera's viewing angle to shift when the object is in some corner. I will try to arrange the colliders in the corners and handle manual collisions. Thanks for the advice. - Nikita Akulov
    • As far as speed is AddForce after AddForce done, AddForce can restrict it via rigid.velocity = Vector2.ClampMagnitude(rigid.velocity, VelocityLimit); and then the movement will be at the same speed. - KingPeas
    • Got it, thank you very much! if there are any more instructions / recommendations I will be grateful - Nikita Akulov