Hi guys, im trying to make a game where i press the mouse buttons, left or right, and make some objects attract or repel. Im using raycast to the object to detect what type of object it is. That part works, but then i dont know how to make the object that the ray hit, attract or repel. How should i move it, etc.
To sum it up. I raycast from camera to mouse, then that ray hits a box lets say, and i want the box to move towards me. Raycast part working and detecting the box, but then how i move the box?
Well… The easiest way I would imagine is to apply a force to the rigidbody of the object in the direction of your magnet. A very small force that you keep applying every frame, and you increase it as the distance reduce.
First I’d do what LightStriker has said then start getting more complicated…
I’m pretty new at coding myself but the way I would go about it is as follows:
Create a script that defines what “magnetism” is in your game (define “charge” as a string value, with “pos”/“neg”/“neutral” being possible values)
Make an if statement that determines which buttons add which type of charge, and which ones take away
Make another if statement that will make objects go torwards another based on charge (or repel)
Lastly once the above works make it so that the rate they attract increases with attraction level (for this you will also have to come up with more than just a string value for the variables in step 1, my guess is to by that point make a new variable that is a float which is the “charge strength” that can be 0-100 or something like that)
I wont make any code for you, but if you do have some that is giving you trouble do put it up here and maybe we can help you.