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)