0
votes

Hello so i want to achive my raycast to follow the gameobject it hit so i can set my vector3 FollowTarget so the vector3 will be aways following the raycast that is following the gameobject that is moving private void

MoveSelectedToCursorPosition()
    {
        RaycastHit raycastHit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out raycastHit, 1000.0f))
        {
            if(raycastHit.collider.tag == "Terrain")
            {
                gameController.UnitSelection.MoveSelectionToPosition(raycastHit.point);
            }


            if(raycastHit.collider.tag == "Minion")
            {           
                FollowTarget = raycastHit.point;
                gameController.UnitSelection.MoveSelectionToPosition(FollowTarget);

            }
        }
    }
1
Raycasts are instantaneous, they can't follow anything.Draco18s no longer trusts SE
can i set the vector 3 to follow the gameobject ? @Draco18sdrugsandwich
Vector3s are immutable primitives, so no. You want a follow target, save a reference to the target's transform.Draco18s no longer trusts SE
yea but i will need the actual vector because im setting it and i cant do a transformdrugsandwich
Save a reference to the transform and query its position value...Draco18s no longer trusts SE

1 Answers

0
votes

RayCast-Follow

A Raycast cannot follow anything. It's an instantaneus event, that casts a ray along 2 given Points in space.

Solution

As Draco18s mentioned in a comment, you can just save a reference to the transform and use the transforms position.

public class Example {
  private Transform _followTarget;

  MoveSelectedToCursorPosition() {
    RaycastHit raycastHit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out raycastHit, 1000.0f)) {
      if (raycastHit.collider.tag == "Terrain") {
        gameController.UnitSelection.MoveSelectionToPosition(raycastHit.point);
      }


      if (raycastHit.collider.tag == "Minion") {
        this._followTarget = raycastHit.collider.transform;
      gameController.UnitSelection.MoveSelectionToPosition(this._followTarget.position);
      }
    }
  }
}