T
T
Topul2022-02-12 01:33:04
C++ / C#
Topul, 2022-02-12 01:33:04

How to make movement constraint on x,y,z?

Hello, I can not do the restriction of movement in any way. (like invisible borders)
The movement is implemented through touching the screen (Android)
The script itself is attached to the camera, the game is in 3D.

public Transform target;
private Vector3 offset;
private float distance;

  void Update() 
  {
 		if (Input.GetMouseButtonDown(0)) 
    {
  			RaycastHit hit;
  			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  			if (Physics.Raycast(ray, out hit)) 
      {
   				offset = target.position - hit.point;
   				distance = hit.distance;
  			}
 		}
 		if (Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0) && target != null) 
    {
  			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  			Vector3 v = ray.direction;
  			target.position = ray.origin + v * distance + offset;
 		}
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2022-02-12
@Griboks

I advise you to "clamp" the coordinates in FixedUpdate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question