C# 使用右箭头键更新相机位置

C# 使用右箭头键更新相机位置,c#,unity3d,C#,Unity3d,您好,在我下面的代码中,我正在使用UI按钮更新我的相机位置,该按钮工作正常。我想做的是执行相同的过程,但按向右箭头键,就像我按向右箭头键一样,相机将其位置更改为点A,然后停止,当我再次按相同的箭头键时,相机将其位置更改为点正如在代码中一样,我有一个不同的函数可以在不同的ui按钮上调用,所以,谢谢,这是我的代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class c

您好,在我下面的代码中,我正在使用UI按钮更新我的相机位置,该按钮工作正常。我想做的是执行相同的过程,但按向右箭头键,就像我按向右箭头键一样,相机将其位置更改为点A,然后停止,当我再次按相同的箭头键时,相机将其位置更改为点正如在代码中一样,我有一个不同的函数可以在不同的ui按钮上调用,所以,谢谢,这是我的代码

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

public class cam : MonoBehaviour
{
    [Header("Locations where camera will update its position step by step")]
    public Transform handleview;
    public Transform needle1view;
    public Transform wallview;
    public Transform handletwoview;
    public Transform needle2view;
    public Transform switchview;
    public Transform lastswitchview;

    public GameObject Animatebtn;
    Animator animatebtnanim;

    [Header("UI Buttons")]
    public GameObject inspectionbtn;
    public GameObject animatebtn;
    public GameObject step2btn;
    public GameObject step3btn;
    public GameObject step4btn;
    public GameObject step5btn;
    public GameObject step6btn;
    public GameObject step7btn;

    [Header("Inspection Views")]
    public Transform startview;
    public Transform handle1view;
    public Transform motorview;
    public Transform handle2view;

    [Header("Move Boolean")]
    public bool move = false;

    [Header("Speed At Which Cam Moves")]
    public float speed;

    [Header("Current View/position Of Camera")]
    Transform currentVIEW;

    [Header("Current Angel Of Camera")]
    Vector3 currentangel;

    [Header("FieldofView of Camera ")]
    public float camFieldOFview = 24f;

    public int track = 0;

    // Use this for initialization
    void Start()
    {
        Camera.main.fieldOfView = camFieldOFview;
        animatebtnanim = Animatebtn.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (move)
        {
            //float step = speed * Time.deltaTime;
            //transform.position = Vector3.MoveTowards(transform.position, currentVIEW.position, step);
            transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * speed);
            currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * speed),
                Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * speed),
                Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * speed));

            transform.eulerAngles = currentangel;
        }

        //if (Input.GetKey(KeyCode.RightArrow))
        //{
        //    if (track == 7)
        //        track = 1;
        //    if (track == 1)
        //    {
        //        moveTOstartVIEW();
        //        move = true;
        //    }
        //    else if (track == 2)
        //    {
        //        moveTOhandleONEview();
        //        move = true;
        //    }
        //    track += 1;

        //}

    }
    // this function will lerp camera to startview location
    public void moveTOstartVIEW()
    {
        currentVIEW = startview;
        move = true;
    }

    public void moveTOhandleONEview()
    {
        currentVIEW = handle1view;
        move = true;
        animatebtnanim.SetBool("start", true);
        animatebtnanim.SetBool("move", false);
    }

    public void moveTOmotorview()
    {
        currentVIEW = motorview;

        move = true;
    }


    public void moveTOhandleTWOview()
    {
        currentVIEW = handle2view;
        move = true;
    }

    public void Handleview()
    {
        currentVIEW = handleview;
        inspectionbtn.SetActive(false);
        animatebtn.SetActive(false);
        move = true;
    }

    public void Needleoneview()
    {
        currentVIEW = needle1view;
        step2btn.SetActive(false);
        move = true;
    }

    public void Wallview()
    {
        currentVIEW = wallview;
        move = true;
        step3btn.SetActive(false);
    }

    public void Handletwoview()
    {
        currentVIEW = handletwoview;
        move = true;
        step4btn.SetActive(false);
    }

    public void Needletwoview()
    {
        currentVIEW = needle2view;
        move = true;
        step5btn.SetActive(false);
    }

    public void Switchview()
    {
        currentVIEW = switchview;
        move = true;
        step6btn.SetActive(false);
    }

    public void lastSwitchview()
    {
        currentVIEW = lastswitchview;
        move = true;
        step7btn.SetActive(false);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类cam:单一行为
{
[标题(“相机将逐步更新其位置的位置”)]
公共交通工具;
公众观点;
公共墙视图;
公共转换视图;
公众观点;
公众视野;
公众视野;
公共游戏对象动画EBTN;
动画师;
[标题(“UI按钮”)]
公共游戏对象检查;
公共游戏对象动画EBTN;
公共游戏对象step2btn;
公共游戏对象step3btn;
公共游戏对象step4btn;
公共游戏对象step5btn;
公共游戏对象step6btn;
公共游戏对象step7btn;
[标题(“检查视图”)]
公众视野;
公共转换手柄1视图;
公众视野;
公共转换处理2视图;
[标题(“移动布尔”)]
公共布尔移动=假;
[收割台(“凸轮移动的速度”)]
公众浮标速度;
[标题(“摄像机的当前视图/位置”)]
转换当前视图;
[标题(“摄影机的当前角度”)]
矢量3电流角;
[标题(“摄像机视野”)]
公共浮动视野=24f;
公共int轨道=0;
//用于初始化
void Start()
{
Camera.main.fieldOfView=camFieldOFview;
animatebtnanim=Animatebtn.GetComponent();
}
//每帧调用一次更新
无效更新()
{
如果(移动)
{
//浮动步长=速度*时间.deltaTime;
//transform.position=Vector3.moveToward(transform.position,currentVIEW.position,step);
transform.position=Vector3.Lerp(transform.position、currentVIEW.position、Time.deltaTime*速度);
currentangel=新矢量3(Mathf.LerpAngle(transform.rotation.eulerAngles.x,currentVIEW.transform.rotation.eulerAngles.x,Time.deltaTime*速度),
Mathf.LerpAngle(transform.rotation.eulerAngles.y,currentVIEW.transform.rotation.eulerAngles.y,Time.deltaTime*速度),
Mathf.LerpAngle(transform.rotation.eulerAngles.z,currentVIEW.transform.rotation.eulerAngles.z,Time.deltaTime*速度);
transform.eulerAngles=currentangel;
}
//if(Input.GetKey(KeyCode.RightArrow))
//{
//如果(磁道==7)
//轨道=1;
//如果(磁道==1)
//    {
//moveTOstartVIEW();
//move=true;
//    }
//else if(磁道==2)
//    {
//moveTOhandleONEview();
//move=true;
//    }
//轨道+=1;
//}
}
//此功能将把摄像机移动到startview位置
公共无效moveTOstartVIEW()
{
currentVIEW=startview;
move=true;
}
公共无效moveTOhandleONEview()
{
currentVIEW=handle1view;
move=true;
animatebtnanim.SetBool(“开始”,true);
animatebtnanim.SetBool(“移动”,false);
}
public void moveTOmotorview()
{
currentVIEW=motorview;
move=true;
}
public void movetohandletwoiview()
{
currentVIEW=handle2view;
move=true;
}
公共无效手册视图()
{
currentVIEW=handleview;
检查BTN.SetActive(假);
animatebtn.SetActive(false);
move=true;
}
公共视图()
{
currentVIEW=needle1view;
步骤2btn.设置激活(假);
move=true;
}
公共墙视图()
{
当前视图=墙视图;
move=true;
步骤3BTN.SetActive(假);
}
public void handletwoiview()
{
currentVIEW=handletwoview;
move=true;
步骤4BTN.SetActive(假);
}
公共视图()
{
currentVIEW=needle2view;
move=true;
步骤5btn.设置激活(假);
}
公共视图()
{
currentVIEW=开关视图;
move=true;
步骤6btn.设置激活(假);
}
public void lastSwitchview()
{
currentVIEW=lastswitchview;
move=true;
步骤7btn.设置激活(假);
}
}

据我所知,您有一个常用的
UI.Button
组件,现在希望在某个键盘键上执行与此按钮在
onClick
中相同的操作

解决方案1:扩展按钮 我只需在获得某个特定值后调用按钮事件,方法是将以下组件放在按钮对象上的
按钮
组件旁边(不在相机上


如果它已经可以与UI一起使用,那么只要在按下向右箭头时调用相同的函数即可。@Eddge我对每个按钮都有不同的函数,然后保持跟踪,设置该值,并使用switch语句调用相应的函数,该函数的按钮会被按下。@Eddge先生,请您将我的代码修改为您所说的内容,因为我是noob,如果您有时间,请试一试,然后,如果您需要帮助,请展示您的尝试。感谢您的回复,正如您在我的代码中看到的,我有很多公共功能用于将相机从一个位置移动到另一个位置,目前我正在使用统一的ui按钮来实现这一点,就像如果我有6个公共功能,那么我也有6个ui b
using UnityEngine;

// make sure you are not accidentely using 
// UnityEngine.Experimental.UIElements.Button
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public class KeyboardButton : MonoBehaviour
{
    // Which key should this Button react to?
    // Select this in the inspector for each Button
    public KeyCode ReactToKey;
    private Button _button;

    private void Awake()
    {
        _button = GetComponent<Button>();
    }

    // Wait for the defined key
    private void Update()
    {
        // If key not pressed do nothing
        if (!Input.GetKeyDown(ReactToKey)) return;

        // This simply tells the button to execute it's onClick event
        // So it does exactly the same as if you would have clicked it in the UI
        _button.onClick.Invoke();
    }
}
using UnityEngine;
using UnityEngine.Events;

public class KeyboardButton : MonoBehaviour
{
    // Which key should this Button react to?
    // Select this in the inspector for each Button
    public KeyCode ReactToKey;

    // reference the target methods here just as 
    // you would do with the Button's onClick
    public UnityEvent OnPress;

    // Wait for the defined key
    private void Update()
    {
        // If key not pressed do nothing
        if (!Input.GetKeyDown(ReactToKey)) return;

        OnPress.Invoke();
    }
}