gpt4 book ai didi

swift - 如何让 Sprite 跟随另一个 "within bounds"

转载 作者:行者123 更新时间:2023-11-28 12:43:15 24 4
gpt4 key购买 nike

我想创建一个关注用户的眼球。从这个意义上讲,眼睛应该跟随用户的位置,但只能在一定范围内移动(眼窝)。我在下面编写的代码有效,但它非常不稳定。

if(dist <= socketRadius - self.aEye.size.width/2.2){
lastPosition = self.aEye.position
self.aEye.physicsBody?.velocity = CGVector(dx:theVec.dx * eyeMoveSpeed, dy:theVec.dy * eyeMoveSpeed)
}
else{

let toCenterVector = normalizeVector( CGVector(dx:(self.socket.position.x - self.aEye.position.x), dy:(self.socket.position.y - self.aEye.position.y)*3 ))
self.aEye.physicsBody?.velocity = toCenterVector
print(toCenterVector)
print("Out")
}

(dist是眼球到眼窝中心的距离)

有没有办法让眼球在眼窝周围顺畅流动以跟随用户的位置?

最佳答案

您可以使用 SKConstraints 来实现这一点。像这样的东西:

let rangeToCenterSprite = SKRange(lowerLimit: 80, upperLimit: 90)
var distanceConstraint: SKConstraint

distanceConstraint = SKConstraint.distance(rangeToCenterSprite, toNode: SpriteNodeInEyeCenter)
EyeBallSprite.constraints = [distanceConstraint]

关于swift - 如何让 Sprite 跟随另一个 "within bounds",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com