简体   繁体   English

我如何使对象将不停地自动向上/向下缩放?

[英]How can I make that the object will scale automatic up/down nonstop?

Now I'm using the F key to make it scale up or down. 现在,我使用F键使其按比例放大或缩小。 But I want to add another method for example AutoScaling that when calling it in Update it will scale up first once finished scaling up it will scale down and then up again and so no nonstop. 但是我想添加另一种方法,例如AutoScaling,当在Update中调用它时,它将首先按比例放大,一旦按比例放大,它将按比例缩小,然后再次按比例放大,因此不间断。

The Scaling script : 缩放脚本:

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

public class Scaling : UnityEngine.MonoBehaviour
{
    public GameObject objectToScale;
    public GameObject lookAtTarget;
    public float duration = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;
    public bool scaleUp = false;
    public Coroutine scaleCoroutine;
    public bool scalingHasFinished = false;

    public void Inits()
    {
        scalingHasFinished = false;
        objectToScale.transform.localScale = minSize;
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, Camera objectToScaleCamera)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            if (scaleUp)
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(lookPos);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }
            else
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(objectToScaleCamera.transform.forward);//SwitchCameras.GetCurrentCamera().transform.forward);//Camera.main.transform.forward);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }

            yield return null;
        }

        scalingHasFinished = true;
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, float rotationSpeed)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            targetObj.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime, Space.Self);

            yield return null;
        }
    }
}

And the script that use the scaling : 和使用缩放的脚本:

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

public class ObjectsManipulation : UnityEngine.MonoBehaviour
{
    //Camera
    public Camera playerCamera;

    //Scaling
    private bool canScale = true;
    private Scaling scaling;

    //Lights
    public DimLights dimlights;
    private Coroutine lightCoroutine;

    //Colors
    private Colors colors;

    //Rotating
    private bool stopRotation = false;
    private Rotating rotating;

    private void Start()
    {
        scaling = GetComponent<Scaling>();
        scaling.Inits();

        colors = GetComponent<Colors>();
        colors.Start();

        rotating = GetComponent<Rotating>();
    }

    // Use this for initialization
    void Update()
    {
        if (playerCamera != null)
        {
            //Scaling
            if (Input.GetKeyDown(KeyCode.F) && canScale == true)
            {
                Scaling();
            }
        }

        //Rotate
        if (Input.GetKey(KeyCode.R) && !scaling.scaleUp)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
            rotating.keyPressed = true;
        }
        if (Input.GetKeyUp(KeyCode.R))
        {
            rotating.keyPressed = false;
        }

        if (!rotating.keyPressed && !scaling.scaleUp && rotating.rotateBack == false
            && DetectInteractable.detected == false)
        {
            scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
        }

        if (DetectInteractable.detected == true && !scaling.scaleUp && stopRotation == false)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
        }
    }

    public void Scaling()
    {
        //Flip the scale direction when F key is pressed
        scaling.scaleUp = !scaling.scaleUp;

        //Stop old coroutine
        if (scaling.scaleCoroutine != null)
            StopCoroutine(scaling.scaleCoroutine);

        if (lightCoroutine != null)
            StopCoroutine(lightCoroutine);

        //Scale  up
        if (scaling.scaleUp)
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            rotating.rotateBack = false;
            scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration, playerCamera));
            if (dimlights.lightsOnOff == false)
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
        }

        //Scale Down
        else
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            rotating.rotateBack = true;
            scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.minSize, scaling.duration, playerCamera));
            if (dimlights.lightsOnOff == false)
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, scaling.duration)); ;
        }
    }
}

And in third script I want to call a method that will be in the ObjectsManipulation script maybe the same method Scaling maybe he will get a bool and if the bool is true make it scaling up/down automatic if it's not true make it use a key. 在第三个脚本中,我想调用一个将在ObjectsManipulation脚本中使用的方法也许是相同的方法缩放也许他会得到一个布尔值;如果布尔值是正确的,则使其自动按比例放大/缩小;如果不正确,则使用键。

This is the script for testing the Scaling : 这是测试扩展的脚本:

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

public class ScalingTest : MonoBehaviour
{
    ObjectsManipulation om;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        om.Scaling();
    }
}

For example in the Update maybe to do : om.Scaling(false); 例如,在Update中可能要做:om.Scaling(false); for using the F key and om.Scaling(true); 用于使用F键和om.Scaling(true); for automatic. 自动。

Update of what I have tried : 我尝试过的更新:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();

    [HideInInspector]
    public int dialogueIndex;

    [HideInInspector]
    public int conversationIndex;

    private bool triggered = false;
    private bool activateButton = false;
    private DialogueManager dialoguemanager;
    private bool startDialogue = false;

    private void Start()
    {
        dialogueIndex = 0;
        dialoguemanager = FindObjectOfType<DialogueManager>();
    }

    public IEnumerator PlayConversation(int index)
    {
        this.conversationIndex = index;

        if (conversations.Count > 0 &&
            conversations[index].Dialogues.Count > 0)
        {
            for (int i = 0; i < conversations[index].Dialogues.Count; i++)
            {
                if (triggered == false)
                {
                    if (dialoguemanager != null)
                    {
                        dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
                    }

                    while (DialogueManager.dialogueEnded == false)
                    {
                        yield return null;
                    }
                }
            }
        }
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
        File.WriteAllText(@"d:\json.txt", jsonTransform);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        conversations.Clear();
        conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
    } 
}

And using it like this : 并像这样使用它:

StartCoroutine(conversationTrigger.PlayConversation(0));

Where conversationTrigger is public ConversationTrigger conversationTrigger; 其中talkingTrigger是public ConversationTrigger conversationTrigger;

But it's not working good at all. 但这根本不好用。 It's starting the conversation index 0 but then play only the first dialogue sentences twice and then never continue to the next dialogue there are two dialogues in this case. 它从对话索引0开始,但随后只播放第一个对话句子两次,然后再也不继续进行下一个对话,在这种情况下有两个对话。 And then it stop. 然后停止。

It should play all the dialogues of the current conversation. 它应该播放当前对话的所有对话。 Something in the PlayConversation method is wrong. PlayConversation方法中的某些问题是错误的。

I'm sorry to say your post is long and contains many redundant codes, so I haven't carefully read it, this is a simple reference about making a zoom animation by script. 抱歉,您的帖子很长,并且包含许多冗余代码,因此我没有仔细阅读,这是有关按脚本制作缩放动画的简单参考。

float minScale; // Minimum scale value
float maxScale; // Maximum scale value
Transform target; // Target to scale

void Update()
{
    float scale = Mathf.PingPong(Time.time, maxScale - minScale) + minScale;
    target.localScale = new Vector3(scale, scale, scale);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM