- Home /
Character Keep On Moving Without Pressing Button
Hey everyone, so I'm new to Unity (It's my first time here) I would like to ask a small question My Player's using a Character Controller and Character Motor with 3rd Person Controller Today, I created a script for a simple platform system using UnityEngine; using System.Collections;
public class MovingPlatformSystem : MonoBehaviour
{
public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;
void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if(transform.position == DestinationSpot.position)
{
Switch = true;
}
if(transform.position == OriginSpot.position)
{
Switch = false;
}
// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch)
{
transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
}
else
{
// If Switch is false, it tells the platform to move to the destination.
transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
}
}
}
The system worked, but the problem is when my character (means the player) is sitting on the platform, he just keep on looping the walk animation without any button pressed. Thus, the animation became faster and slower based on the speed of the platform, so I think it must be a moving script causing the havoc! (And I thought that script must have interpreted the character's position changing as moving) So, could anyone help me with figuring out which script caused the whole problems? Thanks in advanced!
why are you using moveTowards?? If you just want to move the transform, just use transform.Translate. Also, third argument in $$anonymous$$oveTowards is not speed. it is the distance that you want the transform to stop moving before it reaches the target position. This doesn't make any sense with what you are trying to achieve. Another point is, use if-else if ins$$anonymous$$d of two ifs.
if(transform.position == DestinationSpot.position)
{
Switch = true;
}
else if(transform.position == OriginSpot.position)
{
Switch = false;
}
Can you share the player script as well? This will help deter$$anonymous$$e the actual bug.