我想创建一个跟随用户的眼球。从这个意义上讲,眼睛应该跟随用户的位置,但应该只能在一定范围内移动(眼窝)。我在下面写的代码有效,但它非常不稳定。
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
是从眼球到眼窝中心的距离)
有没有办法让眼球在其插座周围顺畅流动以跟随用户的位置?
答案 0 :(得分:2)
您可以使用SKConstraints实现此目的。类似的东西:
let rangeToCenterSprite = SKRange(lowerLimit: 80, upperLimit: 90)
var distanceConstraint: SKConstraint
distanceConstraint = SKConstraint.distance(rangeToCenterSprite, toNode: SpriteNodeInEyeCenter)
EyeBallSprite.constraints = [distanceConstraint]