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

How to Best program Player control in 3D Games

Discussion in 'Getting Started' started by jbb1979, Aug 14, 2019.

  1. jbb1979

    jbb1979

    Joined:
    Aug 6, 2019
    Posts:
    320
    Hello, I have a problem . .

    I found a tutorial for ' roll a ball ' and, it set up a player-controller, by first making a ball, then adding a physics body ( Rigidbody ), so it reacted to physical stuff and, then applied a force to it, short coding, it worked - - But I'm a bit tired, that when I release the button, the ball keeps rolling and, some-how this doesn't feel how you would control a player . .

    I figured out to use the Debug.Log to show the values in ' variables ' and, during play-testing, I saw in console how Input.GetAxis() gave me sort of a ramp, of values, not just " 1 " when I pressed up and, I know I need to use Input.GetAxisRaw to get that, so I'm very happy with that . . I saw another tutorial where I saw some-one use a Transform function to apply a certain movement, along a Vector3, whenever the Input.GetAxis() was active, I assume it works by sending the value of the WSDA direction keys into a Scripted vector and, apply that times a value, to the player . .

    So, that was done under void update() so, it had to be corrected by multiplying with Time.deltaTime, which is the time each frame last, so the longer a frame lasts, the more times the movement vector is applied to the object, correcting for frame-rate . . My question is, why not Just do this in void fixedUpdate(), I mean run Input.GetAxis here, so one avoids having to use Time.deltaTime, it seems very silly to waste ones time . . My problem is, there are two ways of doing things and, I'm terribly unsure which is the right one . . Also, what is the best way to change position of character, I want to be constant, so when he runs up a hill, the length of the displacement vector stays constant and, it feels natural . .

    What is the best code to use for moving the ' Player ' GameObject ( 'me') in the game ? What code should I use - - There are different options and, I don't understand enough, I'm sorry . .. $

    I know there are many youtube tutorials but, they don't explain if they've made the optimal, most Logical choice, they just do stuff and, I have got no idea if it's bad code - -

    I Desperately want the people who have made the engine to tell me how And, Where To use it - -$

    Again, Sorry if This is stupid - -
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    There is no "one best way." The best form of character controller depends on what your character needs to do, and how it needs to interact with the environment.

    Personally, I feel the beginner tutorials often rely too much on the physics engine, leading newbies into a world of pain. Any time you use the physics engine, you are relinquishing some control, and may well end up fighting it to get the behavior you want.

    But in any case, if your character is not a ball that is rolling around, a puck sliding on ice, or a spaceship in space, then you probably should not be moving it by applying forces. Roll-a-ball demonstrated that; now you should put it aside and probably never use that technique again.

    Instead, if you need physics at all, use Rigidbody.MovePosition. And if you don't need physics, rejoice and just assign to transform.position.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Also, you should take large problems and split them up into smaller problems. For example, taking input from the player and using that input to manipulate the character are separate problems to solve. Treating them as a single problem is just going to overwhelm you.
     
    jbb1979 likes this.
  4. jbb1979

    jbb1979

    Joined:
    Aug 6, 2019
    Posts:
    320
    Thanks - -
     
  5. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    306
    I Wish I knew this back when I was a noob... Trying to achieve some behavior with physics resulting in 1+ month of pain and then achieving it within a day after I discovered I didnt really need physics for it and changing transforms is enough...
     
    jbb1979 and JoeStrout like this.