简体   繁体   中英

Rotate a gameobject in Unity

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.

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.

Thanks in advance!

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
        );
    }
}

I get the following error:

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

Try:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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