Flying Pickups

That method does assume that the origin of the gameObject is at the bottom of the model. If the model doesn’t sit correctly on a plane with both the plane and the object at 0,0,0 this script won’t work.

which… it does :slight_smile:

Out of curiosity, are you trying to make this bridge span a gap? Since the Raytrace is from the point of origin, which would be over the gap, this wouldn’t work.

not exactly sure what you mean by “Span a Gap”, but… if we’re talking about covering a specific area, then yes I am :slight_smile:

if it helps though, there is terrain right below it, I just didn’t want it to feel like it was flying (because setting each part of it manually to fit the ground will be a lot of work)

By spanning a gap, I mean if you’re placing a bridge, one part of the bridge is on one end of a gap, and the other end on the other, with a gap under the center of the bridge (which is, of course, the point of a bridge).

If you’re saying that this function isn’t bringing the model to the ground, I don’t know what to say… What is happening at Start()? “That didn’t work” doesn’t tell me anything useful.

this is quite literally all what the function does, I have nothing else left inside of it. I placed this on the prefab that was shown above:

using UnityEngine;

public class SnapToTerrain : MonoBehaviour
{

    void Start() 
    {
        transform.position = RaycastToTerrain(transform.position);
    }

    Vector3 RaycastToTerrain(Vector3 position)
    {
        Ray ray = new Ray(position + Vector3.up * 2, Vector3.down);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 10, LayerMask.GetMask("Terrain")))
        {
            return hit.point;
        }

        else return position;
    }
}

Ok, What happens when the game starts?

to the bridge? It stays in its place

It doesn’t get snapped to the ground by any means

Try this:

using UnityEngine;

public class SnapToTerrain : MonoBehaviour
{
    [ContextMenu("PlaceOnGround")]
    void PlaceOnGround() 
    {
        transform.position = RaycastToTerrain(transform.position);
    }

    Vector3 RaycastToTerrain(Vector3 position)
    {
        Ray ray = new Ray(position + Vector3.up * 2, Vector3.down);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 10, LayerMask.GetMask("Terrain")))
        {
            return hit.point;
        }
        Debug.Log($"No terrain found.  Check terrain's layer and make sure you're within 8 m of the terrain.");
        return position;
    }
}

This will be an edit time script. Click on the Hamburger icon of this component (upper right hand side of component’s inspector) and select PlaceOnGround().

1 Like

OK I’m literally confused as of what in the world is going on here. What exactly should I be doing here? :sweat_smile:

(apologies, but when I restarted Unity earlier today, a weird bug showed up and I’m investigating that too… it has something to do with my swimming states’ Input Reader buttons not reading properly anymore for some reason. It keeps on working long after I lifted my finger off the assigned buttons, and I have no idea why. Three states that look exactly the same. One works, two don’t)

Place the script on the object, and in edit mode, click on the “Hamburger Icon” of the component (the three stacked dots… like you’d stack a hamburger with it’s bun).

Select “PlaceOnGround”


Then stand in awe as the item magically glues itself to the ground

ahh, that worked well… until you do the operation like 5 or 6 times, and now you got a floating island of bridge gaps :stuck_out_tongue:

By the way, does Unity have a button release trigger timer of some sort that is hidden by chance? in other words, it won’t read a button released until ‘x’ seconds are spent holding that button down?

Explain it to me like I have no idea what you’re talking about… pictures mya help.

I’m assuming you’re referring to InputSystem buttons… they hae a life cycle… it should fired “released” as soon as you let go of the button.

lol a video would be doing us a solid one, but pictures will do too:

Before ‘PlaceOnGround’ (NOT in PlayMode. OK I accidentally started it in PlayMode…):

After 6 times of using ‘PlaceOnGround’ (again, NOT in Playmode):

in PlayMode, they all allign perfectly, I know because I accidentally tried it there first, and I thought I was going crazy because it was working with no issues…

let me try restart Unity again, because clearly that’s not what’s happening :sweat_smile: (at least not for some of my buttons. And…?! NOPE, not working still)

Um, you’re not trying to use the function on three objects selected at the same time, are you?

oh that’s going to be a long day then…

Alright let me focus on my other major bugs because some of these need some serious addressing first :sweat_smile:

OK this is something I pulled out of ‘InventorySlotUI.cs’ to avoid spam clicks, and I think it’ll help my issue with the button clicking:

public void OnPointerClick(PointerEventData eventData) {

                if (LastUIClicked != this) {

                    LastUIClicked = this;
                    Invoke(nameof(TimesUp), .001f); // a very small timer means spam clicks are impossible, eliminating potential bugs coming out of that
                    Debug.Log($"{index} was clicked once");

                }

                else {
                    HandleDoubleClick();
                }
            }

            // 'TimesUp' checks if we're the last UI clicked (if true, clear 'LastUIClicked')
            private void TimesUp() {
                if (LastUIClicked == this) LastUIClicked = null;
            }

Any chance we can implement a similar ‘TimesUp()’ for the buttons in the Input Reader action map? I want to make sure that he doesn’t go into running or other states if the button is not being held down (I’m not sure why the normal one stopped working, but… yes it did stop working properly, and sometimes it’ll stay in states it’s not supposed to be in, pretty much permanently until I click the button again)

Most likely, you’re forgetting to unsubscribe from a button event in an Exit() somewhere.
For every event subscription, there must be an unsubscription.

the buttons with the trouble are not states that we convert to, they are part of the states themselves. So… in simple terms, when I click the shift, I am doing the changing in ‘PlayerFreeLookState.cs’ itself. I’m not shifting to a ‘PlayerFreeLookRunningState.cs’ state for example. Here’s an example of what I mean:

            if (stateMachine.InputReader.IsSpeeding)
            {
                stateMachine.Animator.SetFloat(FreeLookSpeedHash, 2 * movement.magnitude, AnimatorDampTime, deltaTime);
                FaceMovementDirection(movement, deltaTime);
                Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);
                return;
            }

This is an extra codeblock I placed in ‘PlayerFreeLookState.Tick()’. Do I really have to switch to new states and use events for that? (and now I have the problem of ‘where did ‘deltaTime’ come from?’)

You shouldn’t, but clearly at this point I don’t have the slightest clue what is going on. Your description of the problem didn’t include this. There’s a limit to what I can fix without information.

What are you trying to accomplish?
What steps did you take to accomplish this?
What happens when you do it?"

Privacy & Terms