Unity3d Unity Smooth 2D摄像头跟踪-重大问题:(
正如你在主题中所看到的-我有一个相机问题。我使用一个脚本(你可以在下面看到),我有这样的东西- 我想要实现的是一个平滑的摄像机到播放器的跟踪。 我什么都试过了。我有大约50-60帧的帧速,但这个错误还是发生了 这是我的相机代码:Unity3d Unity Smooth 2D摄像头跟踪-重大问题:(,unity3d,camera,smooth,Unity3d,Camera,Smooth,正如你在主题中所看到的-我有一个相机问题。我使用一个脚本(你可以在下面看到),我有这样的东西- 我想要实现的是一个平滑的摄像机到播放器的跟踪。 我什么都试过了。我有大约50-60帧的帧速,但这个错误还是发生了 这是我的相机代码: void Update() { if(!player) return; //if(!isDiggableCamera) { Vector3 point = Camera.main.WorldToViewportPoint(player.transfo
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);
}
//移动播放器
浮动可变速度x=5000.0f;
浮子温度=6000.0f;
Vector2速度=新Vector2(x*Time.deltaTime*changableSpeedX,
y*Time.deltaTime*change);
//如果(playerRigidbody.velocity.y+speed.y>=Game.Game().activeMaxVelY)
//speed.y=Game.Game().activeMaxVelY-playerRigidbody.velocity.y;
playerRigidbody.AddForce(速度);
//加力(速度);
//检查最大速度
受保护的无效检查速度()
{
如果(playerRigidbody.velocity.x>Game.Game().activeMaxVelX)
playerRigidbody.velocity=新矢量2(Game.Game().activeMaxVelX,playerRigidbody.velocity.y);
if(playerRigidbody.velocity.x<-Game.Game().activeMaxVelX)
playerRigidbody.velocity=新矢量2(-Game.Game().activeMaxVelX,playerRigidbody.velocity.y);
如果(playerRigidbody.velocity.y>Game.Game().activeMaxVelY)
playerRigidbody.velocity=新矢量2(playerRigidbody.velocity.x,Game.Game().activeMaxVelY);
if(playerRigidbody.velocity.y
有人能帮我吗
如果您需要更多代码,请告诉我是哪一部分(因为我不想添加太多不必要的代码)我可以建议在更新功能中使用lerp sir吗
maincamera.transform.position = new vector3(maincamera.transform.position,player.transform.poistion,speed*Time.deltaTime);
试试这个
}非常抱歉,我想帮助您,但我无法访问web player atm(公司规则),您能告诉我发生了什么事吗?问题在于摄像头跟随播放器。一切都在跳跃,而不是平稳进行。我尝试了“插值”但仍然是同样的问题。其他人告诉我,在“Update()”中移动对象可能有问题,但在FixedUpdate()中可能有问题是一样的…好吧,稍后我会看游戏,但原因可能是,在你的代码逻辑中,相机的转换是通过计算一定的速度来实现的,并且由于对象的速度在一次更新中发生变化,你只能在下一帧得到更新,依此类推。。。
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);
}