【问题标题】:Object following Player对象跟随玩家
【发布时间】:2015-04-12 04:16:03
【问题描述】:

我在 Unity 中乱搞,想制作一个机制,让盒子接触另一个对象,然后该对象会跟随玩家。

我的 Cube 设置如下:

还有一个带有相同选项的 Box Collider 的 Sphere。

我的播放器脚本是这样的:

public class Player : MonoBehaviour {

public float speed = 0.0f;
public float moveX = 0.0f;
public float moveY = 0.0f;
public GameObject player;
public GameObject obj;
//public float force = 0.0f;
private bool collided = false;


// Use this for initialization
void Start () {
    player = GameObject.FindWithTag ("Player");
}

// Update is called once per frame
void FixedUpdate () {
    moveX = Input.GetAxis ("Horizontal");
    moveY = Input.GetAxis ("Vertical");
    player.GetComponent<Rigidbody> ().velocity = new Vector2 (moveX * speed, moveY * speed);    
}

void OnCollisionEnter (Collision col) {
    if (col.gameObject == obj) {
        collided = true;
    }
}

void OnCollisionExit (Collision col) {
    if (col.gameObject == obj) {
        collided = false;
    }
}

void Update () {
    if(collided) {
        obj.transform.position = (player.transform.position - obj.transform.position)*speed;
    }
}

}

我还没有做什么?希望有人能把我推向正确的方向。

【问题讨论】:

标签: c# unity3d


【解决方案1】:

我将为您提供两个脚本。

第一个脚本是 FollowTarget。这将有力地跟随你的目标。

第二个脚本是 SmoothFollow,它将跟随您的目标平滑移动。

FollowTarget.cs

using System;
using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    public Transform target;
    public Vector3 offset = new Vector3(0f, 7.5f, 0f);


    private void LateUpdate()
    {
        transform.position = target.position + offset;
    }
}

SmoothFollow.cs

using UnityEngine;


public class SmoothFollow : MonoBehaviour
{

    // The target we are following
    [SerializeField]
    private Transform target;
    // The distance in the x-z plane to the target
    [SerializeField]
    private float distance = 10.0f;
    // the height we want the camera to be above the target
    [SerializeField]
    private float height = 5.0f;

    [SerializeField]
    private float rotationDamping;
    [SerializeField]
    private float heightDamping;

    // Use this for initialization
    void Start() { }

    // Update is called once per frame
    void LateUpdate()
    {
        // Early out if we don't have a target
        if (!target)
            return;

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

        var currentRotationAngle = transform.eulerAngles.y;
        var 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
        var 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 = new Vector3(transform.position.x ,currentHeight , transform.position.z);

        // Always look at the target
        transform.LookAt(target);
    }
}

只需选择其中之一,然后将其附加到游戏对象。就像另一个应该跟随的盒子一样。

删除脚本中的 Update() 函数,因为您不再需要它了。

同时删除你的 OnCollisionExit()

void OnCollisionEnter (Collision col) {
    if (col.gameObject == obj) {

        // If you choose to use SmoothFollow Uncomment this.
        //col.GetComponent<SmoothFollow>().target = this.transform;

        // If you choose to use FollowTarget Uncomment this
        //col.GetComponent<FollowTarget>().target = this.transform;
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    相关资源
    最近更新 更多