Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作

Unity 自动旋转动画

1.开门需要门把手先动,门再动
2.关门需要门先动,门把手再动
3.中途播放过程中不可以再次进行操作
觉得太复杂?查看我的文章开关门简易进阶版

效果:

请添加图片描述
如果这个门可以直接打开的话,就不需要放置"门把手"
如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的
可调整参数有:角度,反向,轴向,速度
运行时点击Test进行测试
在这里插入图片描述

自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考
上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ObjectController_Rotate : MonoBehaviour
{
    
    
    [Header("门把手")]
    public ObjectController_Rotate objectController_Door;
    [Header("旋转角度")]
    public float angle = 90f;
    [Header("反向")]
    public bool reversal = false;
    [Header("旋转速度")]
    public float openSpeed = 100;
    [Header("旋转轴向")]
    public bool xAxial, yAxial, zAxial;

    float menAngle = 0;
    public Action PlayAction;
    public Action EndAction = null;

    [HideInInspector]
    public bool state = false;

    /// <summary>
    /// 是否可操作
    /// </summary>
    [HideInInspector]
    public bool ActivateControl = true;
    Vector3 angleStart = Vector3.zero;
    Vector3 angleEnd = Vector3.zero;
    protected void Start()
    {
    
    
        angleStart = transform.eulerAngles;
        Vector3 vector = Vector3.zero;
        if (xAxial)
            vector.x = angle;
        if (yAxial)
            vector.y = angle;
        if (zAxial)
            vector.z = angle;
        angleEnd = transform.eulerAngles + vector;
    }
    public bool test = false;
    private void Update()
    {
    
    
        if (test)
        {
    
    
            Control();
            test = false;
        }
    }
    public void Control()
    {
    
    
        if (IsControl())
        {
    
    
            if (objectController_Door)
            {
    
    
                if (objectController_Door.state == false)
                {
    
    
                    objectController_Door.PlayAction = PlayOpenDoor;
                    objectController_Door.Control();
                    ActivateControl = false;
                }
                else
                {
    
    
                    objectController_Door.PlayAction = null;
                    PlayOpenDoor();
                }
            }
            else
            {
    
    
                PlayOpenDoor();
            }
        }
    }
    void ActivateDoor()
    {
    
    
        ActivateControl = true;
        if (EndAction != null)
            EndAction();
    }

    public bool IsControl()
    {
    
    
        if (ActivateControl == false)
            return false;
        return true;
    }
    void PlayOpenDoor()
    {
    
    
        Open = true;
    }
    bool open = false;
    public bool Open
    {
    
    
        get => open;
        set
        {
    
    
            if (value)
            {
    
    
                ActivateControl = false;
            }
            else
            {
    
    
                ActivateControl = true; ;
            }
            open = value;
        }
    }
    private void LateUpdate()
    {
    
    
        if (Open == true)
        {
    
    
            int i = 1;
            if (reversal)
                i = -1;
            Vector3 vector = Vector3.zero;
            if (xAxial)
                vector.x = openSpeed * Time.deltaTime;
            if (yAxial)
                vector.y = openSpeed * Time.deltaTime;
            if (zAxial)
                vector.z = openSpeed * Time.deltaTime;
            if (state == false)
            {
    
    
                transform.Rotate(vector * i);
            }
            else
            {
    
    
                transform.Rotate(vector * -1 * i);
            }

            menAngle += openSpeed * Time.deltaTime;
            if (menAngle > angle)
            {
    
    

                if (state == false)
                {
    
    
                    if (reversal)
                    {
    
    
                        vector = Vector3.zero;
                        if (xAxial)
                            vector.x = angle;
                        if (yAxial)
                            vector.y = angle;
                        if (zAxial)
                            vector.z = angle;
                        transform.eulerAngles = angleEnd - vector * 2;
                    }
                    else
                        transform.eulerAngles = angleEnd;
                    state = true;
                    Open = false;
                }
                else
                {
    
    
                    transform.eulerAngles = angleStart;
                    state = false;
                    Open = false;
                    if (objectController_Door)
                    {
    
    
                        if (objectController_Door.state)
                        {
    
    
                            objectController_Door.Control();
                            ActivateControl = false;
                            if (objectController_Door.EndAction == null)
                                objectController_Door.EndAction = ActivateDoor;
                        }
                    }
                    else
                    {
    
    
                        ActivateDoor();
                    }
                }
                menAngle = 0;
                OnPutCallBack();
            }
        }
    }
    public void OnPutCallBack()
    {
    
    
        if (PlayAction != null)
            PlayAction();
    }
}

Unity 自动移动动画

移动途中不可进行操作

效果:

请添加图片描述
可调整参数有:距离,反向,方向,速度,单次循环
在这里插入图片描述
搞!

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

public class ObjectController_Move : MonoBehaviour
{
    
    
    [Header("移动距离")]
    public float distance = 0.5f;
    [Header("反向")]
    public bool reversal = false;
    [Header("移动速度")]
    public float moveSpeed = 1;
    [Header("移动方向")]
    public bool xDirection, yDirection, zDirection;
    [Header("单次循环")]
    public bool isLoop = false;
    Vector3 Direction = Vector3.zero;
    Vector3 StartPos = Vector3.zero;
    /// <summary>
    /// 当前的状态
    /// </summary>
    [HideInInspector]
    public bool state = false;
    /// <summary>
    /// 是否可操作
    /// </summary>
    [HideInInspector]
    public bool ActivateControl = true;
    protected void Start()
    {
    
    
        StartPos = transform.position;
        int i = 1;
        if (reversal)
            i = -1;
        if (xDirection)
            Direction = transform.right* distance;
        if (yDirection)
            Direction = transform.up * distance;
        if (zDirection)
            Direction = transform.forward * distance;
        Direction *= i;
        Direction += transform.position;
    }
    public bool test = false;
    private void Update()
    {
    
    
        if (test)
        {
    
    
            Control();
            test = false;
        }
    }
    bool Move = false;
    public void Control()
    {
    
    
        if (IsControl())
        {
    
    
            Move = true;
            ActivateControl = false;
        }
    }
    public bool IsControl()
    {
    
    
        if (ActivateControl)
        {
    
    
            return true;
        }
        return false;
    }
    private void LateUpdate()
    {
    
    
        if (Move)
        {
    
    
            if (state)
            {
    
    
                if (StartMove(StartPos))
                {
    
    
                    state = false;
                }
            }
            else
            {
    
    
                if (StartMove(Direction))
                {
    
    
                    state = true;
                    if (isLoop)
                        Move = true;
                }
            }

        }
    }
    bool StartMove(Vector3 EndPos)
    {
    
    
        transform.position = Vector3.Lerp(transform.position, EndPos, moveSpeed * Time.deltaTime);
        if (Vector3.Distance(transform.position, EndPos) < 0.05f)
        {
    
    
            transform.position = EndPos;
            Move = false;
            ActivateControl = true;
            return true;
        }
        return false;
    }
}

不会真的有人想要Demo吧

猜你喜欢

转载自blog.csdn.net/CTangZe/article/details/125271210