Unity 3 made my game bug

Hi! I was making a game in unity 2,6 and it when I upgraded to Unity 3, it was all buggy. It's a very simple game with a cube as a character, with a script very indentical to the FPSwalker script that follows with unity. The problem is that when I run the game, the character bugs at some random points. it just "jumps" a little distance forward, and the rotation bugs sometimes too, because it just rotate the character with 90*. I dont get any console errors, and when I change my script with the FPSwalker script, it bugs too. Any other people experiencing this too?

oh, also when I jump, it runs forward very fast...

Here is the script. Its VERY messed up, but this is my first game, so I hope you can help me.

ps. Stim pack is something i got from starcraft. It basically just should make the character to move faster for a little time.

var SPpower = 10.0;
var stimpacktime = 2.0;
var stimpack : boolean = false;

// Variables for rotation function
private var rotation = Vector3.zero;
var rotationspeed = 6.0;

//Variables for movement function
private var moveDirection = Vector3.zero;
var movespeed = 6.0;

// For Jumping
var jumpstrength = 5;
var gravity = 5;
private var grounded : boolean = false;

// Start of the script
function FixedUpdate()
{
if(SPpower < 0)
        {

        stimpack = false;

        }

// Stim pack
if(Input.GetKeyDown("x"))
{
stimpack= !stimpack;
if(SPpower < 0)
{
stimpack = false;
}
}

if(grounded)

moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);

moveDirection *= movespeed * Time.deltaTime * 10;
// Adding stim packs
if(stimpack)
{
moveDirection = moveDirection * stimpacktime;
}

// rotation
rotation = new Vector3(0,Input.GetAxis("Horizontal"),0);
rotation *= rotationspeed * Time.deltaTime;

if(stimpack)// Adding stim packs
{
rotation = rotation * stimpacktime;
 }

transform.Rotate(rotation);

// Jump
if(Input.GetButton("Jump")) 
{
moveDirection.y = jumpstrength;
}
}

// Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;

if(stimpack == true && SPpower > 0)
{
SPpower -= Time.deltaTime * 2;
}

if(stimpack == false && SPpower < 10)
{

SPpower += Time.deltaTime * 0.5;
}

if(SPpower < 0)
        {
    stimpack = false;   
        }

}

@script RequireComponent(CharacterController)

I have made a new script out of the two you sent. Can you please check it?

My new script:

    var SPpower = 10.0; 
    var stimpacktime = 2.0; 
    var stimpack : boolean = false; 

    // Variables for rotation function
    private var rotation = Vector3.zero;
    var rotationspeed = 6.0;
    rotationspeed = rotationspeed * 10; 

    //Variables for movement function
    private var moveDirection = Vector3.zero;
    var movespeed = 6.0;
    movespeed = movespeed * 15; 

    // For Jumping
    var jumpstrength = 5;
    var gravity = 5;

    var grounded : boolean;

    // Stimpack function

    function stimpackdetect()
    {
    if(stimpack == true && SPpower < 0.5) 
    {
        stimpack = false;
        }

    if(stimpack == true && SPpower > 0)
    {
    SPpower -= Time.deltaTime * 2;
    }

    else if(stimpack == false && SPpower < 10) 
    {

    SPpower += Time.deltaTime * 0.5;
    }

    }

    // Start of the script
function FixedUpdate () 
{

    if(Input.GetKeyDown("x")) 
    {
    stimpack = !stimpack;

    }

    stimpackdetect(); 

    if(grounded)
{
    moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);

    moveDirection *= movespeed * Time.deltaTime * 10;
    // Adding stim packs
    if(stimpack)
    {
    moveDirection = moveDirection * stimpacktime;
    }

    // rotation
    rotation = new Vector3(0,Input.GetAxis("Horizontal"),0);
    rotation *= rotationspeed * Time.deltaTime;

    if(stimpack)// Adding stim packs
    {
    rotation = rotation * stimpacktime;
     }

    transform.Rotate(rotation);

    // Jump
    if(Input.GetButton("Jump")) 
    {
    moveDirection.y = jumpstrength;
    }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        var controller : CharacterController = GetComponent(CharacterController);
        var flags = controller.Move(moveDirection * Time.deltaTime);
        grounded = (flags & CollisionFlags.CollidedBelow) != 0;

        }

     @script RequireComponent(CharacterController) 

if u compare a var stimpack,just use else:

   if(stimpack == true && SPpower > 0)
{
SPpower -= Time.deltaTime * 2;
}

else if(stimpack == false && SPpower < 10)
{

SPpower += Time.deltaTime * 0.5;
}

and in the open function Fixed update,u call 3 times thisc check:

if(SPpower < 0)
{
stimpack = false;

i think it's no correct.

i add correct code, thinks:

    var SPpower = 10.0;
    var stimpacktime = 2.0;
    var stimpack : boolean = false;

    // Variables for rotation function
    private var rotation = Vector3.zero;
    var rotationspeed = 6.0;

    //Variables for movement function
    private var moveDirection = Vector3.zero;
    var movespeed = 6.0;

    // For Jumping
    var jumpstrength = 5;
    var gravity = 5;
    private var grounded : boolean = false;

    // Start of the script
    function FixedUpdate()
    {
    if(SPpower < 0)
            {

            stimpack = false;

            }

    // Stim pack
    if(Input.GetKeyDown("x"))
    {
    stimpack= !stimpack;

    }

    if(grounded)

    moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);

    moveDirection *= movespeed * Time.deltaTime * 10;
    // Adding stim packs
    if(stimpack)
    {
    moveDirection = moveDirection * stimpacktime;
    }

    // rotation
    rotation = new Vector3(0,Input.GetAxis("Horizontal"),0);
    rotation *= rotationspeed * Time.deltaTime;

    if(stimpack)// Adding stim packs
    {
    rotation = rotation * stimpacktime;
     }

    transform.Rotate(rotation);

    // Jump
    if(Input.GetButton("Jump")) 
    {
    moveDirection.y = jumpstrength;
    }
    }

    // Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        var controller : CharacterController = GetComponent(CharacterController);
        var flags = controller.Move(moveDirection * Time.deltaTime);
        grounded = (flags & CollisionFlags.CollidedBelow) != 0;

    //i think IF IT DON'T WORK,JUST CREATE FUNCTION OF THIS AND CALL WHEN YOU ADD STIMPACK

    if(stimpack == true && SPpower > 0)
    {
    SPpower -= Time.deltaTime * 2;
    }

    else if(stimpack == false && SPpower < 10)
    {

    SPpower += Time.deltaTime * 0.5;
    }

    }

@script RequireComponent(CharacterController)

Your code isn't 'illegal', but it is very bad. Just because it's legal to stick you face into a fire doesn't make it a good idea.

  • Your first problem is that you are missing a brace after the `if(grounded)`.
  • Your second problem is because of your first problem that you are closing the FixedUpdate brace after your jump.

To avoid mistakes like these, please indent according to your scope - most editors should do this for you automatically.

  • Your third problem is that your moveDirection is only multiplied by deltaTime if grounded. In subsequent, non-grounded frames, the deltaTime could have changed. It works only because it is in FixedUpdate, but is generally bad.
  • Do you want to keep jumping every frame that you are holding the jump button? If not, you should use GetButtonDown.
  • Is your moveDirection a set speed or an acceleration? If it's a set speed, you don't need to multiply it by deltaTime twice.

Here is a modified version of the script that should work a little more as you might expect:

//Stim Pack
var stimPackTime : float = 5.0;
var stimPackMultiplier : float = 2.0;
var stimPack : boolean = false;

//Turning
var baseRotationSpeed : float = 6.0;
private var rotationSpeed : float = baseRotationSpeed;
private var rotation : Vector3 = Vector3.zero;

//Movement
var baseMoveSpeed : float = 60.0;
private var moveSpeed : float = baseMoveSpeed;
private var moveDirection : Vector3 = Vector3.zero;

//Jumping
var jumpstrength : float = 5.0;
var gravity : float = 5.0;
private var grounded : boolean = false;

function FixedUpdate() {

    //Turn stim pack on/off
    if(Input.GetKeyDown("x")) stimPack = !stimPack;

    //Apply stim pack
    if(stimPack && stimPackTime > 0) {
        moveSpeed = baseMoveSpeed * stimPackMultiplier;
        rotationSpeed = baseRotationSpeed * stimPackMultiplier;
        stimPackTime -= Time.deltaTime;

        if(stimPackTime <= 0) stimPack = false; //automatic shut-off once used up
    }
    //Don't apply stim pack
    else {
        moveSpeed = baseMoveSpeed;
        rotationSpeed = baseRotationSpeed;

        //Regenerate stim pack when not in use at a rate of 1/4 second regen/second   
        if(!stimPack && stimPackTime < 5.0)
            stimPackTime += Time.deltaTime * 0.25;
    }

    //You can never have more than 5 or less than 0 seconds of stim pack
    stimPackTime = Mathf.Clamp(stimPackTime, 0.0, 5.0);

    //On the ground
    if(grounded) { //You were missing this brace
        //Turn first
        rotation = Vector3(0,Input.GetAxis("Horizontal"),0) * rotationSpeed * Time.deltaTime;
        transform.Rotate(rotation);

        //Move in the new direction
        moveDirection = transform.forward * Input.GetAxis("Vertical") * moveSpeed;

        // Jump
        if(Input.GetButtonDown("Jump")) moveDirection.y = jumpstrength;
    }
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)