Crane movement

hello gents,

I’m beginner in unity and I write a script that control a crane movement it’s work perfect, but the problem is no limitation for arrow movement.

here is the script.

public class Crane : MonoBehaviour
{
    public float TurnSpeed;


    public GameObject craneTop; 
    public GameObject Hook;
    public GameObject Top1; // Arrow 1
    public GameObject Top2; // Arrow 2


  

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.D))
            TurnCrane();
       
        if (Input.GetKey(KeyCode.A))
            TurnCraneLeft();

        if (Input.GetKey(KeyCode.W))
            UP();

        if (Input.GetKey(KeyCode.S))
            DOWN();

        if (Input.GetKey(KeyCode.R))
            UP_1();

        if (Input.GetKey(KeyCode.E))
            DOWN_1();
    }

    public void TurnCrane()
    {
        craneTop.transform.Rotate(Vector3.forward, TurnSpeed * Time.deltaTime); 
    }

    public void TurnCraneLeft()
    {
        craneTop.transform.Rotate(Vector3.back, TurnSpeed * Time.deltaTime);

    }

    public void UP()
    {
        Top1.transform.Rotate(Vector3.left, TurnSpeed * Time.deltaTime); 
    }

    public void DOWN()
    {
        Top1.transform.Rotate(Vector3.right, TurnSpeed * Time.deltaTime);
    }

    public void UP_1()
    {
        Top2.transform.Rotate(Vector3.left, TurnSpeed * Time.deltaTime);
    }

    public void DOWN_1()
    {
        Top2.transform.Rotate(Vector3.right, TurnSpeed * Time.deltaTime);
    }

}

what arrow movements?

anyway if you want to limit the crane rotation just add some checks to verify if the crane is outside the rotation limits.

Just yesterday there was a thread on limiting rotations. I wrote an example on constraining the rotation of a FPS camera here: Camera Rotation Limits - #7 by spiney199

The same principle applies in all situations really Store your rotation values separate from the game object, modify and then constrain said values, before applying them to the game object.

1 Like

sorry for not explain everything

thank you so much. I will try to use it on my script

1 Like

ok so you have a 3D object not a simple thing with boxes. I suggest using a solution already created by unity that is made for these types of situations. To automate the animation and provide limits and all that stuff, is called animation rigging. they have various demos so check how they did. I’ve once animated a similar situation, a crane truck, and it works. takes some time to figure out the details but once you understand how it works is very powerful.

https://docs.unity3d.com/Packages/com.unity.animation.rigging@1.0/manual/index.html