Hello guys im trying to create limit for movement range of my player. I set tag for ground and target for buildings. I want only move my object if ray hit ground. But i have problem about it.
In this picture, when i clicked the intersection between ground and building object still running. How can i handle this problem? I think i need to change this part of code because every object intersection with plane make my object running. ( Intersection part shown as yellow in picture)
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
void Update()
{
ConsumeEnergy();
if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
SetTargetPosition();
if (isMoving)
CowMovement();
}
public void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point) && hit.collider.gameObject.tag == "Ground")
{
targetPosition = ray.GetPoint(point);
isMoving = true;
}
}
// ------------------------------------------- Part VI ------------------------------------------------
public void CowMovement()
{
transform.LookAt(targetPosition);
transform.position = Vector3.MoveTowards(transform.position,
targetPosition,
speed * Time.deltaTime);
{
if (transform.position == targetPosition)
{
isMoving = false;
}
}
Debug.DrawLine(transform.position, targetPosition, Color.red);
}