But I’m kind of lost on how to begin the code. I tried to do some stuff, but doesn’t work at all. Can anyone try to explain to me step by step what I’ll need and if possible some help with the code?
P.S.: On this example, the mouse make the particle “run away”, in my project it will be a GameObject (Cube)
Hmm, this looks like fun. I’ll do an example for you in a few minutes… Until then, I’d start by casting a ray straight down from the camera, and doing a Physics.OverlapSphere at the hitpoint to check for any objects within a radius. Obviously all the objects in the radius will be moved away from that radius. When the object is no longer within the radius, it will move back, etc. Try writing all of the steps your MouseScript and MoveableObject scripts will have to take when you mouse over the screen one time.
Okay, so you’re just wanting the run away/move back functionality? If so, then I’ll just do it my way (with a cube), and keep the cube at the mouses position. I’m sure you can figure it out from there. Working on it now!
Okay man, this should be everything you asked for… If the mouse isn’t receiving input, then it will still come to a rest. If the mouse is receiving input it will move away from the cube. Let me know if this isn’t what you want, and I’ll modify it to your liking. I used rigidbodies on the spheres to give it a more realistic vibe. The spheres act as if they are under a gravitational force. If you feel like they are moving around a bit too much, you can adjust the “Drag” property on all of their rigidbodies to a higher value. The higher the Drag, the faster they will fall to a stop. I tried to child all of the spheres to one empty gameobject, but since I’m using their local position, it really messed up the physics, so unfortunately I left it with a bunch of spheres in the project hierarchy. I’ll have to fix that whenever I get the chance.
P.S: The Wireframe shader on the ObjectMover cube will mess up a bit and lose it’s transparency… Idk why this happens, but what fixed it for me was just deleting the Wireframe shader from the Materials folder and then going to the link below and copy/pasting the shader code into a new shader. That will give the transparency back to you.
Oh, by the way… you will need to use all 3 scripts included in the “Scripts” folder. The first, entitled “InputDetector” should be on an empty gameobject. Preferably your GameManager or something. All it does is hold a static bool that I make a call to to see if we are receiving input or not. I used that to make sure that they go back into place even if the cube is over them while not receiving input.
Next is the ObjectMover script. This script should be applied to any object you want to do the moving. In your case, you would put it on the cube.
Last but not least is the MoveableObject script. This is the script where all the magic happens. It should be applied to anything you want your ObjectMover to move. In your case, you would put it on your spheres.
You should look at my scene and see if all your stuff is oriented correctly. I think I made it to where they will only move on the x and z axes. My camera was looking down on the plane in the Y axis. So, if your camera is looking toward the camera on the Z axis, then you would want them to move on the X and Y, etc… I’ll change the scripts up a bit so they can work on all orientations.
Okay man, here’s a build that’ll work on all orientations.
I made one last script which is a singleton and threw it on the SphereHolder object. You can choose in the inspector whether the orientation is Up, Down, Left, Right, Front, or Back.
This means that if your camera is pointing down at the spheres on the Y axis, then you want to use Up, or Down. There’s virtually no difference in what is done there. You could choose either.
If your camera is pointing at the spheres on the X axis, you want to use Left or Right.
Finally, if your camera is pointing at the spheres on the Z axis, you want to use Front or Back.
Pretty much, all I do is check to see which orientation you have selected and I assign the constraints of the rigidbody accordingly.
I also put all of the necessary objects into prefabs so you can easily drag them into scenes. So, once you drag your SphereHolder object onto the scene you need to click it and make sure your Orientation is set the right way. If it is setup the right way, then they will all move how they are supposed to…
If you intended the movement to be more 3D, and you wanted them to move on all axes, that’ll require a bit more work but it’s do-able. Idk if it’ll look good though.