7

Ball というオブジェクトがあり、それにキーボードの対話機能 (ボールを動かすための WASD) を追加しました。 カメラが背後に留まり、ボールを追跡する必要がありますが、エラーが発生します。

using UnityEngine;
using System.Collections;
public class ballmain : MonoBehaviour {
    public bool isMoving = false;
    public string direction;
    public float camX;
    public float camY;
    public float camZ;
    // Use this for initialization
    void Start () {
        Debug.Log("Can this run!!!");
    }

    // Update is called once per frame
    void Update () {
        camX = rigidbody.transform.position.x -=10;
        camY = rigidbody.transform.position.y -=10;
        camZ = rigidbody.transform.position.z;
        camera.transform.position = new Vector3(camX, camY, camZ);
            //followed by code that makes ball move
    }
}

「Assets/ballmain.cs(18,44): エラー CS1612: 'UnityEngine.Transform.position' の値型の戻り値を変更できません。値を一時変数に格納することを検討してください」というエラーが表示されます。カメラに関するコードをコメントアウトすると、ボールが動き回ります。

4

6 に答える 6

6

どうぞ 。完全なコード。

シンプルフォロー

using UnityEngine;
using System.Collections;

public class Follow: MonoBehaviour {
public Transform target;
public float smooth= 5.0f;
void  Update (){
    transform.position = Vector3.Lerp (
        transform.position, target.position,
        Time.deltaTime * smooth);
} 

    } 

高度なフォロー

using UnityEngine;
using System.Collections;

public class SmoothFollowScript: MonoBehaviour {

// The target we are following
public  Transform target;
// The distance in the x-z plane to the target
public int distance = 10.0;
// the height we want the camera to be above the target
public int height = 10.0;
// How much we 
public heightDamping = 2.0;
public rotationDamping = 0.6;


void  LateUpdate (){
    // Early out if we don't have a target
    if (TargetScript.russ == true){
    if (!target)
        return;

    // Calculate the current rotation angles
    wantedRotationAngle = target.eulerAngles.y;
    wantedHeight = target.position.y + height;

    currentRotationAngle = transform.eulerAngles.y;
    currentHeight = transform.position.y;

    // Damp the rotation around the y-axis
    currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

    // Damp the height
    currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

    // Convert the angle into a rotation
    currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

    // Set the position of the camera on the x-z plane to:
    // distance meters behind the target
    transform.position = target.position;
    transform.position -= currentRotation * Vector3.forward * distance;

    // Set the height of the camera
    transform.position.y = currentHeight;

    // Always look at the target
    transform.LookAt (target);
}
}
}
于 2013-11-10T12:54:18.660 に答える
1

このシンプルで便利な Unity 2D カメラ フォロー スクリプトを見つけました。

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
    targetPos = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
    if (target)
    {
        Vector3 posNoZ = transform.position;
        posNoZ.z = target.transform.position.z;

        Vector3 targetDirection = (target.transform.position - posNoZ);

        interpVelocity = targetDirection.magnitude * 5f;

        targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

        transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

    }
   }
}

ソースunity2d カメラ フォロー スクリプト

于 2015-02-27T12:35:27.437 に答える
0

これら-=の行の:

   camX = rigidbody.transform.position.x -=10;
   camY = rigidbody.transform.position.y -=10;

間違っている。は-=を変更しようとしrigidbody.transform.positionます。あなたはただ欲しい-

ただし、現状では、カメラはターゲットのZ位置の変化を追跡しません。また、カメラを回転させた場合、カメラは適切に追跡しません。必要な正しい位置を取得するには(ベクトルで):-

cam_pos = target_pos - dist_to_target * cam_look_at
于 2012-05-25T10:24:16.940 に答える