Infinite loop problems

Updated on May 30, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on May 28, 2017

Hi I’m trying to update my place holder movement to a more advanced one. I’m using a unity free asset and trying to implement it. Then once I have it working properly I’m going to make changes. The only problem is a bunch of functions are going on an infinite loop in playmode. I can’t move my character, but I can jump at will. While jumping I can move my character, but once landed it stops.

 

I’ts a long script so I’ll link only a bit. I can link the lot if needed/wanted.

Use a unity asset, it will be easy they said…lol.

protected virtual void LateUpdate()
{
InputHandle ();

}

void FreeMovement()
{
// set speed to both vertical and horizontal inputs
speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
speed = Mathf.Clamp(speed, 0, 1);
// add 0.5f on sprint to change the animation on animator
if (isSprinting && input != Vector2.zero) speed += 0.5f;
if (stopMove) speed = 0f;
if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
{
Vector3 lookDirection = targetDirection.normalized;
freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
var diferenceRotation = freeRotation.eulerAngles.y – transform.eulerAngles.y;
var eulerY = transform.eulerAngles.y;

// apply free directional rotation while not turning180 animations
if (isGrounded || (!isGrounded && jumpAirControl))
{
if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
var euler = new Vector3(0, eulerY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), freeRotationSpeed * Time.deltaTime);
}
}
}

 

protected virtual void InputHandle()
{
Debug.Log (“InputHandle”);

if (!lockMovement)
//if (PauseMenu.IsOn)
//return;
{
MoveCharacter ();
SprintInput ();
StrafeInput ();
JumpInput ();
}

}

 

protected virtual void MoveCharacter()
{
input.x = Input.GetAxis(“Horizontal”);
input.y = Input.GetAxis(“Vertical”);
Debug.Log (“MoveCharacter”);
}

 

P.S

Sorry for being a pest asking many questions.

  • Liked by
Reply
1 on May 29, 2017

If you want to make a more advanced script, I recommend starting from scratch.

on May 30, 2017

It behaves the same way in a fresh project as well. The problem is a logic problem. I’m executing the code endlessly, instead of on an input or when a certain state is active. Yet the check I have for that seems to not work.

Show more replies
  • Liked by
Reply
Cancel
0 on May 30, 2017

protected virtual void Start ()
{
Init ();
}

protected virtual void LateUpdate()
{
InputHandle ();

}
protected virtual void FixedUpdate()
{
AirControl ();
}

protected virtual void Update()
{
UpdateMotor ();

}

public virtual void UpdateMotor()
{
CheckGround();
ControlJumpBehaviour();
ControlLocomotion();
StopMove();
Debug.Log (“UpdateMotor”);
}

protected bool freeLocomotionConditions
{
get
{
if (locomotionType.Equals(LocomotionType.OnlyStrafe)) isStrafing = true; Debug.Log (“freeLocomotionConditions”);
return !isStrafing && !locomotionType.Equals(LocomotionType.OnlyStrafe) || locomotionType.Equals(LocomotionType.OnlyFree);
//Debug.Log (“freeLocomotionConditions”);
}
}

void ControlLocomotion()
{
if (freeLocomotionConditions)
FreeMovement(); // free directional movement
else
StrafeMovement(); // move forward, backwards, strafe left and right
Debug.Log(“ControlLocomotion”);
}

protected void ControlSpeed(float velocity)
{
if (Time.deltaTime == 0) return;

if (useRootMotion)
{

}
else
{
var velY = transform.forward * velocity * speed;
velY.y = controller.velocity.y;
var velX = transform.right * velocity * direction;
velX.x = controller.velocity.x;

if (isStrafing)
{
Vector3 v = (transform.TransformDirection(new Vector3(input.x, 0, input.y)) * (velocity > 0 ? velocity : 1f));
v.y = controller.velocity.y;
controller.velocity = Vector3.Lerp(controller.velocity, v, 20f * Time.deltaTime);
}
else
{
controller.velocity = velY;
controller.AddForce(transform.forward * (velocity * speed) * Time.deltaTime, ForceMode.VelocityChange);
Debug.Log (“ControlSpeed”);
}
}
}

protected virtual void InputHandle()
{
Debug.Log (“InputHandle”);

if (lockMovement)

return;
{
MoveCharacter ();
SprintInput ();
StrafeInput ();
JumpInput ();
}

}

protected virtual void MoveCharacter()
{

input.x = Input.GetAxis(“Horizontal”);
input.y = Input.GetAxis(“Vertical”);
Debug.Log (“MoveCharacter”);
}

protected virtual void StrafeInput()
{
if (Input.GetKeyDown(“StrafeInput”))
Strafe();
Debug.Log (“StrafeInput”);
}

protected virtual void SprintInput()
{
if (Input.GetKeyDown(“SprintInput”))
Sprint(true);
else if(Input.GetKeyUp(“SprintInput”))
Sprint(false);
Debug.Log (“SprintInput”);
}

protected virtual void JumpInput()
{
if (Input.GetKeyDown (“JumpInput”))

Jump();
Debug.Log (“JumpInput”);
}
}

 

More of the code and where the issues seems to be. 

 

inputhandler
movecharacter
sprintinput
strafeinput
jumpinput
updatemotor
freelocomotioncondition
controllocomotion
checkground
grounddistance

 

Code that’s always being executed.

  • Liked by
Reply
Cancel