Sending an object a certain distance unless it collides with something?

I’m fairly new to C#/Unity and have been at this for a couple of days. I’m attempting to send a created object wherever the player aims at to a certain range/speed and stop once it gets there. Unless of course it collides with an object (wall, floor, etc) in which case it will stop at that object. I’ve managed to be able to send it to a predetermined location using an empty object but only while holding the fire button. Also tried using ray casting but that just sent it as far as the next object in which case it also clipped about halfway through the object it collides with. If that doesn’t make any sense then hopefully the following does:

  1. Left mouse click.
  2. Object created.
  3. Object travels to a certain range at predetermined speed.
  4. Object stops at max range or an object it collides with.
  5. Right mouse click
  6. Created objects return to player.

Any help would be much appreciated! :smile:

  1. Unity - Scripting API: Input.GetMouseButtonDown

  2. https://docs.unity3d.com/Manual/CreateDestroyObjects.html

  3. This depends on how you want your object to move. Do you want it to move towards the mouse position, or? You will probably want something like this:

  1. When the object is created, store its initial position. Then compare the distance between the initial position to its current position every Update frame. If its greater than or equal to its max range, stop the object. You could do this a number of ways, probably the easiest is to simply set its velocity to 0:
GetComponent<RigidBody>().velocity = Vector3.zero

To stop at an object use this: Unity - Scripting API: Collider.OnTriggerEnter(Collider)

  1. Same as (1)

  2. What do you mean by ‘return to player’? Do you want it to come back to the Player? If so:

Object.transform.position = Player.transform.position

like this?