【问题标题】:Unity Smooth 2D Camera following - Huge Issue :(Unity Smooth 2D Camera following - 巨大的问题:(
【发布时间】:2018-08-03 20:16:34
【问题描述】:

正如您在主题中看到的 - 我遇到了相机问题。我使用了一个脚本(你可以在下面看到),我有这样的东西 - http://rapidgamesstudio.com/games/diggermachines/

我想要实现的是对播放器的平滑跟随相机。 我什么都试过了。我有大约 50-60 fps,但仍然出现此错误。

这是我的相机代码:

void Update() {

if(!player)
    return;

//if(!isDiggableCamera) {
    Vector3 point = Camera.main.WorldToViewportPoint(player.transform.position);
    Vector3 delta = player.transform.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
    Vector3 destination = transform.position + delta;
    destination.z = transform.position.z;
    transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);

//Vector3 destination = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
//transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);

//} else {
//    startDigging(0, 0, 0);
//}

leftSite.position = new Vector3(leftSite.position.x, player.position.y, leftSite.position.z);
rightSite.position = new Vector3(rightSite.position.x, 

player.position.y, rightSite.position.z);
}

我尝试在 Update()、FixedUpdate()、LateUpdate() 中执行此代码,即使是所有这三个 - 仍然是同样的问题。

以下更新玩家位置的代码:

//move player

float changableSpeedX = 5000.0f;
        float changableSpeedY = 6000.0f;
        Vector2 speed = new Vector2(x * Time.deltaTime * changableSpeedX,
                                    y * Time.deltaTime * changableSpeedY);


        //if(playerRigidbody.velocity.y + speed.y >= Game.game().activeMaxVelY)
        //    speed.y = Game.game().activeMaxVelY - playerRigidbody.velocity.y;

        playerRigidbody.AddForce(speed);
            //AddForce(speed);


//and checking max speed

protected void checkSpeed()
    {
        if(playerRigidbody.velocity.x > Game.game().activeMaxVelX)
            playerRigidbody.velocity = new Vector2(Game.game().activeMaxVelX, playerRigidbody.velocity.y);

        if(playerRigidbody.velocity.x < -Game.game().activeMaxVelX)
            playerRigidbody.velocity = new Vector2(-Game.game().activeMaxVelX, playerRigidbody.velocity.y);


        if(playerRigidbody.velocity.y > Game.game().activeMaxVelY)
            playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, Game.game().activeMaxVelY);

        if(playerRigidbody.velocity.y < maxSpeedYGravity)
            playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, maxSpeedYGravity);
    }

谁能帮帮我?

如果您需要更多代码,请告诉我哪一部分(因为我不想添加太多不必要的代码)

【问题讨论】:

  • 非常抱歉,我想提供帮助,但我无法访问网络播放器 atm(公司规则),您能告诉我发生了什么吗?
  • 问题在于相机跟随播放器。一切都在跳跃而不是顺利。我尝试了“插值”,但仍然是同样的问题。其他人告诉我,在“Update()”中移动对象可能有问题,但在 FixedUpdate() 中是一样的......
  • 好吧,稍后我会看到游戏,但原因可能是,在您的代码逻辑中,相机的转换是通过计算一定的速度进行的,并且由于对象的速度在一次更新中有所不同,您只能在下一帧中获得该更新,依此类推...

标签: unity3d camera smooth


【解决方案1】:

我可以建议一个 lerp 先生,在更新功能中使用这个

  maincamera.transform.position = new vector3(maincamera.transform.position,player.transform.poistion,speed*Time.deltaTime);

【讨论】:

    【解决方案2】:

    试试这个!!!

     private float xMax;
    [SerializeField]
    private float yMax;
    [SerializeField]
    private float xMin;
    [SerializeField] 
    private float yMin;
    private Transform target;
    
    // Use this for initialization
    void Start () {
        target = GameObject.Find("Player").transform;   
    }
    
    // Update is called once per frame
    void LateUpdate () {
        transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(target.position.y, yMin, yMax),transform.position.z);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多