Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Forklift Scripting help

Discussion in 'Scripting' started by Chiyou, Oct 31, 2015.

  1. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    Hey there, I'm really new to Unity and so far have only been able to do some really basic scripting thanks to some youtube tutorials, I'm primarily a 3D artist and love creating assets for unity, I've been trying to do some really basic coding to bring a world i'm trying to create together but am finding it difficult to find solutions to my problems with tutorials so i thought i'd come here for help.

    I'm trying to create a basic forklifting game where you drive around the forklift and move cargo from one spot to another. So far i've been able to get the forklift driving around but am stuck with these problems:

    - Having the forks move from one position to three other positions (I would like it so that the forks are at lowered position and when you press the "r" key they raise to mid position, then when pressed again high position, then back to mid, and then back to low.)

    - I've tried using box colliders and rigid body to be able to pick up the cargo container but it creates a huge array of problems while driving around as the container always breaks free from the forks. Using Continuously Dynamic Collision detection seems to help a little bit but does not fix the problem.

    - I would like my chain to be able to move, but I modelled it as one piece of geometry. Is there any kind of built in deformer or rigging system i can use to have the chain follow along the pulley's as the forks go up and down?
     

    Attached Files:

    • Help.jpg
      Help.jpg
      File size:
      1,005.8 KB
      Views:
      872
  2. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    good lookin models.
    I would find and preset variable stating at what Y position the fork should be compared to it's parent (the forklift) setup and Vector3 lowPoint, Vector3 midPoint, and Vector3 highPoint if that's what you want and have it lerp or movetowards a value over time when the player changes the setting which is another variable: currentForkHeight, could be an int: 0,1, 2; or an enum: low, mid, high. have a script watching the currentForkHeight and moving the fork using FixedUpdate and Time.deltaTime.

    the container breaks free how? goes flying? falls through? make sure you play with the rigidbody mass and drag settings.

    getting a chain to move is certainly possible, but it you want to move the mesh through code it's a pretty penny... if you a rig a bones system to it with weight some basic colliders you could pull on the upcoming nearest chain link, pulling it through some sort of loop/guide by adding upward/diagonal force to it. the rest would try to follow as best the could, but it would leave room for bugs pretty easily and stuff getting stuck in there etc, could be fun though. Setting up an animation for it might be smarter.
     
    Chiyou likes this.
  3. UbiquitousStudio

    UbiquitousStudio

    Joined:
    Oct 11, 2015
    Posts:
    24
    You will want to re think the way picking up objects works. The physics won't allow you to simply just pick up a box and balance it like that while driving around. Think of it like real life physics. You put a coffee on top of a vehicle and drive it will fall off, it's the same concept with the box and forklift. Now there is a few ways to go about doing it. The way I would do it is this.

    First you want to split up the model of the forklift to have the arms separate. This way you can easily move them up and down through a script by moving the transform values. You will need to make a variable to tell your script a limit of moving the arms up and down to so they don't just keep moving.

    Now about the box physics. You will want a blank gameobject on the top of the forklift arms and one in the bottom of the box. You will need to code it so that if the collider of the bottom of the box hits the collider of the top of the forklift bars you will want to stick them together. This is a rough explanation because this is really beyond forum explanation and will take lots of messing around.

    Getting the boxes off would be up to you, set a button to do it or make it so if the bottom of the forklift bars touch the ground you simple disable the pick up script. That would make it droppable technically. However like I said there is many ways to go about it.
     
    Chiyou likes this.
  4. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    Thank you guys so much for your help, So far i'm trying the Vector3 method of getting the forks to move (by following this guys tutorial
    ) however i've run into a problem where the forks float around as as they change position. the rotation seems right but as I drive around they get left behind and follow the forklift as it changes the position
     
  5. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    I've found that this piece of code is kind of doing what I want


    function Update()
    {
    if(Input.GetKeyDown("r"))
    {
    transform.Rotate (0,20,0);
    }
    }


    however it seems like there's no way to move the position of the forks instead of the Rotation without using Vector3 which causes some serious problems as i drive around
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ever driven a forklift? Losing the load happens.

    If you want accurate load physics you will need to build a pallet collider and put the load on top of that. This will be computationally expensive, tread cautiously.

    If accuracy doesn't bother you, simply parent the load to the forks and ditch the physics altogether.
     
  7. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    yeah I've driven a forklift, the problem is that the collision gets all messed up as I take turns and the forks will eventually break free of the box colliders. I would definitely prefer to use physics but it seems to create too many issues
     
  8. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    so this bit of script has worked really wll so far, I just have to figure out how to limit how much it can raise or lower

    function Update()
    {
    if(Input.GetKey("r"))
    {
    transform.Translate(Vector3(0,.04,0));
    }
    if(Input.GetKey("f"))
    {
    transform.Translate(Vector3(0,-.04,0));
    }
    }
     
  9. Chiyou

    Chiyou

    Joined:
    May 5, 2014
    Posts:
    7
    okay my forks work properly going up and down with this code:

    function Update()
    {
    if(Input.GetKey("r"))
    {
    transform.Translate(Vector3(0,.04,0));
    transform.position.y = Mathf.Clamp(transform.position.y, 0.08, 2);
    }
    if(Input.GetKey("f"))
    {
    transform.Translate(Vector3(0,-.04,0));
    transform.position.y = Mathf.Clamp(transform.position.y, 0.08, 2);
    }
    }

    however it only works as long as i'm on the flat ground plane, would anyone know how to solve this issue?
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Use transform.up instead of a hard coded vector.
     
  11. DamianBudzakDev

    DamianBudzakDev

    Joined:
    Jun 4, 2020
    Posts:
    1
    how to do it in this concept?