Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Turn-based physics

Discussion in 'Scripting' started by Odineye, Nov 28, 2012.

  1. Odineye

    Odineye

    Joined:
    Nov 16, 2012
    Posts:
    4
    I'm working on proof of concept for some turn-based physics game. Imagine if you will the PoC; A large cube sits in space, the cube is a rigidbody. Turn time begins to countdown. When the large cube is clicked a small cube becomes x-ray visible. The large cube is no longer selectable, only the small cube. The small cube is dragged around until Turn time is 0. The large cube moves with force relative to the distance of the small cubes and towards its last position for the duration of play time. When play time ends the scene freezes for more input.

    Definitions
    Turn Time: A static scene time (in seconds) where the player may decide on the actions they wish to take by manipulating the interaction object (turnTime)
    Play Time: An active scene time (in seconds) where the player can not interact and observes the results of their choices (playTime)
    Large Cube: Player's character object (forceObject)
    Small Cube: The Player's interaction object (addForceObject)

    OPTION: The force can either presist through turns or it can be reset to 0,0,0. I'm not picky, but doing both would make fore fun game options.

    I'm not an experienced coder so I have tried to convey my idea is the best generic form of 'code like' I can conceive.

    Code (csharp):
    1.  
    2. forceObject
    3. addForceObject
    4. playTime
    5. turnTime
    6. force
    7. addForce
    8.  
    9. update
    10.     if (turnTime <= 0) { //do (addForce +- force) for new force of object and run play time
    11.     else { //get forceObject.location as local (0,0,0) then get addForceObject.location in forceObject's local space
    I think I have been doing things the hard way so I am going to ask for help before I waste time with mucked up code.
     
  2. Goblox

    Goblox

    Joined:
    Nov 3, 2012
    Posts:
    178
    Alright well, I've had a couple beers and I won't pretend to understand the game concept but I'll give some hopefully helpful hints.

    1) You can bring your physics objects to a halt by setting their rigidbody.velocity = vector3.zero
    2) Find the force vector required to attract one object to another by subtracting their positions, normalizing the result, and then using a multiplier.
    3) Use FixedUpdate (Not update) for Physics calculations
    4) Multiply by Time.deltaTime where appropriate to account for inconsistent frame rate.