how to make object follow mouse but exclude movement on the Y axis?
I've got a particle system that I'm trying to use as a sort of ground target location for the spawning of another particle system, like one of them RPG AOE spell targeting systems. So far with what info I've managed to find that wasn't in JavaScript (most of which seemed outdated unless JavaScript allows you to alter values in a way that C# doesn't).
I have one script that handles the spawning of my AOE "spell" and within that script I fire a ray cast from the mouse position and if it hits the floor I spawn my targeting particle system, then in another script attached to the targeting particle system I update it's transform position to the current mouse position set at 10.0f away from the camera on the Z axis. This works beautifully and follows the mouse along very smoothly however I can't seem to prevent the particle system from moving on the Y axis, I don't want it to move up off the floor and I certainly can't have it moving through the floor.
Here's the bit from my spawning script:
if(Input.GetButtonDown("Attack2"))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.transform.CompareTag("Floor"))
{
Pooly.Spawn(beamTargetClone, hit.point, Quaternion.identity);
}
}
}
And heres the code attached to the targeting particle system that makes it follow the mouse position:
public class VFX_BeamTarget_CursorFollow : MonoBehaviour
{
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10.0f;
gameObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
I've tried setting mousePos.y to 1.0f for example just something that would certainly be above ground yet this only seems to affect the position the particle system is instantiated in, whereas setting mousePos.z seems to lock the transform in that position after instantiation. I'm really confused at this point. Please any info is greatly appreciated!
Answer by SageGlaze · Mar 24, 2019 at 06:51 PM
Ok I'm trying not to make a habit of answering my own questions I'm beginning to feel like I'm spamming unity answers with silly questions, so for that I apologize! I realized that I should have been using a raycast to move my particle system along the surface of the floor and it's now working great! There are now only a couple of undesirable behaviors with the particle system moving around the environment but I will certainly be burning the midnight oil before I post another question :)