简体   繁体   中英

Making an object move towards user in swift

This might be a simple question for experienced game developers but I'd like to get some advise as I'm new. I'm working on a game which requires an object to move towards the player (or user) to create the perspective of moving towards the object on the road. For this purpose, I have created some code which makes the object move on a straight line in an angle and I also am changing the scale of the object from small to big.

It seems OK but not perfect - especially I can't seem to control the speed of the movement very well. The code is itself very messy. Is there any code snippets or advise on better ways to do this? Any help will be much appreciated. Thanks!

Update: snippet of my "messy" code

//cow_left.position = CGPoint(x: self.size.width/2 + 100 + k_new, y: self.size.height/2 + 150) cow_left.position = CGPoint(x: self.size.width/2 + 100 + k_new, y: self.size.height/2 + 150) cow_left.setScale(sk_new) cow_left.zPosition = 0.2 //k_new = k_new + 2.5 k_new = k_new + 4 sk_new = sk_new+0.002 j_new = 0 //loop_done = 1 //print("In the loop") //addChild(cow_right) print(sk) if (k_new>700) { loop_done_new = 0 k_new = 10.0 sk_new = 0.3} }

Basically what I'm trying to achieve here is to move the sprite from a certain point in an angle towards the player. At the same time I am increasing the scale of the sprite while it moves in this angle to create the perspective of the player moving towards it. A simple task I suppose... but is there a better and cleaner way of doing it?

SceneKit will create a full 3D world. Just setup an SCNCamera for the perspective you prefer. Then use SCNActions to move the objects in any of the x, y, or z axis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM