【问题标题】:How to increment Yposition value for maincamera in unity3d如何在unity3d中增加主摄像机的Y位置值
【发布时间】:2017-07-01 09:58:51
【问题描述】:

我有一个场景,我想统一增加主摄像头的 y 位置值,但它不像附加到主摄像头的脚本,而是通过从另一个游戏附加的脚本来增加主摄像头的 y 位置值-object。有人可以在这方面帮助我吗?

对于使用附加到主相机对象的脚本来增加 Y 位置值,我猜想编写如下代码: Debug.Log(transform.position.y); transform.Translate(Vector3.up * 4, Space.World);

但我应该通过从另一个游戏对象附加的脚本来增加主摄像头的 y 位置值。

【问题讨论】:

  • 您可以使用Camera.main.transform.position 访问标记有主摄像头的摄像头,因此您将编写类似Camera.main.transform.Translate(Vector3.up * 4, Space.World); 的内容

标签: c# unity3d


【解决方案1】:

将以下脚本附加到所需的游戏对象,然后选择您已附加脚本的游戏对象并将相机游戏对象拖放到Target字段中

using UnityEngine;

public class MoveTarget : MonoBehaviour
{
    // Drag & Drop the camera you want to move
    public Transform Target ;

    // Specify the movement you want to apply
    public Vector3 movement = new Vector3( 0, 4, 0 ) ;

    private void Start()
    {
         // Automatically retrieve the Camera tagged "MainCamera" if no target specified
         if( Target == null )
             Target = Camera.main.GetComponent<Transform>();
    }

    private void Update()
    {

         if( Target == null )
            Debug.LogError("No target specified !");
         else
             Target.Translate( movement * Time.deltaTime, Space.World ) ;
    }    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多