Object not following mouse position

Hello Good morning.

I am working on a script where it will let the user build their own levels, Now this script does work but I have one issue that I cannot rap my head around. For some reason the game object will not follow the mouse position can anyone help me.

P.S. I am still new to C#

CODE HERE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Blueprint : MonoBehaviour
{
    RaycastHit hit;
    Vector3 movePoint;
    public GameObject prefab;

    void Start()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 50000.0f, (1 << 8)))
        {
            transform.position = hit.point;
        }
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 50000.0f, (1 << 8)))
        {
            transform.position = hit.point;
        }

        if (Input.GetMouseButton(0))
        {
            Instantiate(prefab, transform.position, transform.rotation);
            Destroy(gameObject);
        }
    }
}

ThankYou
Jacob W.

This is a little bit ambiguous.

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

“For some reason the game object will not follow the mouse position”

You are doing a raycast and then setting the object to which this script is attached to, to the location of ray hit point.

If you press mouse button 0, you instantiate your “prefab” to the location where your GameObject with this script is currently. And immediately you destroy your GameObject.

So which one of these GameObjects you expect to be dragged? Object with the script or the prefab object you instantiated?

Hello,

So this script is for the blueprint, It should outline the object that you want to place then once you have moved your mouse to where you want to place the object, Then you press key 0 Which will then place the real object (The game object prefab verbal is the real building) The Blueprint block which is where the script will be attached to should be following the mouse so then the player can freely pick where they want to place it. For the player to get a new blueprint he will open a GUI which has already been added. I hope this helps

Thanks for responding
Jacob W.

Your code should work.

But…

Make sure your “blueprint” object isn’t blocking rays and also make sure that you have a proper layer…

Drop that bit shifting thing and use LayerMask myMask instead and fill it from inspector to be sure you have correct layer mask in use for ground detection.

// Select layer(s) from Inspector
[SerializeField] LayerMask mask;

// Raycast
if (Physics.Raycast(ray, out hit, 50000f, mask))

If it still doesn’t work, then you have something wrong with your collision setup.

ThankYou

Turns out I was being a bit dumb and forgot to mark the layer to the ground, That fixed. Thank you for bring up layer masks because I would have never thought of that.

Thankyou so much
Jacob W.

1 Like

I suspected that. I personally always use LayerMask fields, but seems like some tutorials for example use that bit shifting to set layer.

Yes they do, But in the future I use layermasks, thanks again for all the help!