在Unity中旋转游戏对象 [英] Rotate a gameobject in Unity

查看:71
本文介绍了在Unity中旋转游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为Unity编写脚本,该脚本获取游戏对象A的位置和旋转并将其使用C#分配给游戏对象B.

Trying to write a script for Unity that takes the position and rotation of game object A and assigns it to game object B using C#.

调试日志显示了我想要获得的正确旋转角度,但是我不知道如何将该值实际分配给另一个游戏对象.

The debug log shows the correct rotation angle that I'm wanting to get, but I don't know how to actually assign that value to the other game object.

从今天开始,我对C#还是陌生的,所以这很可能是我的语法,但是对Unity来说我还是很新.

I'm brand new to C# as of today, so it could very well be my syntax, but I'm also fairly new to Unity.

提前谢谢!

using UnityEngine;
using System.Collections;

public class MoveArrow : MonoBehaviour {

    void Start () {
    }

    void Update () {
        var playerMapPos = GameObject.FindWithTag ("Player");
        var playerWorldPos = GameObject.FindWithTag ("PlayerCube");

        Debug.Log ("x: " + playerMapPos.transform.eulerAngles.x ); 
        Debug.Log ("y: " + playerMapPos.transform.eulerAngles.y );
        Debug.Log ("z: " + playerMapPos.transform.eulerAngles.z );

        playerWorldPos.transform.rotation = Vector3(
            playerMapPos.transform.eulerAngles.x,
            playerMapPos.transform.eulerAngles.y,
            playerMapPos.transform.eulerAngles.z
        );
    }
}

我收到以下错误:

Assets/MoveArrow.cs(24,53):错误CS0119:表达式表示预期为type', where a变量',value' or方法组'

Assets/MoveArrow.cs(24,53): error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected

推荐答案

尝试:

void Update()
{
    var playerMapPos = GameObject.FindWithTag ("Player");
    var playerWorldPos = GameObject.FindWithTag ("PlayerCube");
    playerWorldPos.transform.rotation = playerMapPos.transform.rotation;
}

您尝试执行的操作不起作用的原因是transform.rotation是四元数,而transform.eularAngles是Vector3.

The reason that what you are trying to do isn't working is that transform.rotation is a Quaternion, whilst transform.eularAngles is a Vector3.

这篇关于在Unity中旋转游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆