- Home /
Constant change of direction
Been working on this code for the past few days, making it simpler and simpler.
I removed the parts where it controls jumping and other functions, to find the root of the issue, but maybe I really can't see it.
What it should do:
Keep going right
Once reached the limit (x = 12), change direction
Once reached the other limit (x = -12), change direction again
Repeat
What it's doing:
Keep going right
Once reached the limit (x = 12), change direction
Something forces it to change direction again, but because X is > 12, it sticks on the spot, infinitely changing direction
Really hope you guys can see what I can't...
using UnityEngine;
using System.Collections;
public class NewPlatformerPhysics : MonoBehaviour
{
public float accelerationWalking = 35;
public float accelerationSprinting = 60;
public float currentAcceleration;
float direction = 1.0f;
public float gravityMultiplier = 3.5f;
bool mSprinting = false;
float distToGround;
public float wallLeft = -12.0f;
public float wallRight = 12.0f;
public bool canMove = true;
public bool isGrounded;
public void Start ()
{
}
void FixedUpdate ()
{
ApplyGravity();
UpdateJumping();
if (canMove)
Walk(direction);
}
// Direction -1.0f is left, and 1.0f is right
public void Walk(float direction)
{
if (mSprinting) // Check if player is sprinting
currentAcceleration = accelerationSprinting;
else
currentAcceleration = accelerationWalking;
if (transform.position.x > wallRight) // Once the player touchers the boundaries, change direction
direction = -1.0f;
else if (transform.position.x < wallLeft)
direction = 1.0f;
if (direction < 0) // Animate based off direction
SendAnimMessage("GoLeft");
else if (direction > 0)
SendAnimMessage("GoRight");
rigidbody.AddForce(Vector3.right * direction * currentAcceleration, ForceMode.Acceleration);
}
public bool IsSprinting() { return mSprinting; }
public void StartSprint()
{
mSprinting = true;
SendAnimMessage("StartedSprinting");
}
public void StopSprint()
{
mSprinting = false;
SendAnimMessage("StoppedSprinting");
}
void ApplyGravity()
{
rigidbody.AddForce(Physics.gravity * gravityMultiplier, ForceMode.Acceleration);
}
void SendAnimMessage(string message)
{
SendMessage(message, SendMessageOptions.DontRequireReceiver);
}
void OnCollisionEnter(Collision coll)
{
if(coll.gameObject.tag == "Floor")
{
isGrounded = true;
print ("entering floor");
}
}
void OnCollisionExit(Collision coll)
{
if(coll.gameObject.tag == "Floor")
{
isGrounded = false;
print ("leaving floor");
}
}
}
Answer by SirCrazyNugget · Jun 16, 2014 at 02:52 PM
after changing the direction set the position to the limit too
if (transform.position.x > wallRight){
direction = -1.0f;
Vector3 pos = transform.position;
pos.x = wallRight;
transform.position = pos;
}else if(transform.position.x < wallLeft){
direction = 1.0f;
Vector3 pos = transform.position;
pos.x = wallLeft;
transform.position = pos;
}
Unfortunately that made it worse. Now it changes direction every frame as it doesn't have to stop
Answer by GalanoDev · Jun 17, 2014 at 03:31 AM
Tricky problem you've got: You are nor following proper code standards and, beleive it or not, that's the cause of the error. You should always call member variables as "this.variable" in order to avoid ambiguities for the compiler (and even further you should add a "m_" or the like to its declaration, to lessen the risks even further. Therefore your member variables cold be called "m_Speed" for example).
Your problem is that inside the Walk function you are changing the "direction" variable but, because of scope and since Walk already has an enter parameter called "direction", the compiler only changes the variable on the local scope and not the member of the class. Rename one of those and it will work. Also notice that the direction parameter of Walk is irrelevant since you already defined one in your class and will only cause you troubles and headaches. You should remove it completely and create proper get and set functions for your variable if you ever need to change it! Good luck in your game
Answer by InsaneGoblin · Jun 17, 2014 at 04:38 AM
Solved:
// Direction -1.0f is left, and 1.0f is right
public void Walk(float direction)
{
if (mSprinting) // Check if player is sprinting
currentAcceleration = accelerationSprinting;
else
currentAcceleration = accelerationWalking;
if (transform.position.x > wallRight) // Once the player touches the boundaries, change direction
walkingDirection = -1.0f;
else if (transform.position.x < wallLeft)
walkingDirection = 1.0f;
if (walkingDirection < 0) // Animate based off direction
SendAnimMessage("GoLeft");
else if (walkingDirection > 0)
SendAnimMessage("GoRight");
rigidbody.AddForce(Vector3.right * direction * currentAcceleration, ForceMode.Acceleration);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613151657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers
Understanding Space.World and Space.Self: Character changing movement direction 1 Answer
How to fix/clamp rigidbody dir vector, despite Force dir 1 Answer
Rigidbody regenerated acceleration movement ? 0 Answers
Change rigidbody direction 1 Answer