Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the speed of the agent dynamically #240

Open
kevinNejad opened this issue Aug 10, 2022 · 1 comment
Open

Change the speed of the agent dynamically #240

kevinNejad opened this issue Aug 10, 2022 · 1 comment

Comments

@kevinNejad
Copy link

Hi,
I'm wondering if it is possible to modify the speed in which the agent walks/runs dynamically/programmatically ?
For example, speed up/down according to a function while the agent is interacting in the environment.

Thank you very much for your support,

@krtimisra67
Copy link

yes u can

  1. Unity (C#)
    In Unity, you can modify the agent's speed by adjusting its NavMeshAgent or CharacterController speed dynamically:
    Example
    using UnityEngine;
    using UnityEngine.AI;

public class AgentSpeedControl : MonoBehaviour
{
public NavMeshAgent agent;
public float baseSpeed = 3.5f;
public float speedMultiplier = 2.0f;
private float timeElapsed = 0f;

void Start()
{
    agent.speed = baseSpeed;
}

void Update()
{
    timeElapsed += Time.deltaTime;
    agent.speed = baseSpeed + Mathf.Sin(timeElapsed) * speedMultiplier;
}

}

  1. Unreal Engine (Blueprint or C++)
    In Unreal Engine, you can modify an AI agent's speed by adjusting its Max Walk Speed dynamically.
    Blueprint Approach:
    • In the AI Character's Event Graph, use Set Max Walk Speed and connect it to an event (like a timer or input).

c++ approach
void AMyAICharacter::AdjustSpeed(float DeltaTime)
{
float NewSpeed = BaseSpeed + FMath::Sin(GetWorld()->TimeSeconds) * SpeedMultiplier;
GetCharacterMovement()->MaxWalkSpeed = NewSpeed;
}

  1. OpenAI Gym / Reinforcement Learning Environments
    If you're using reinforcement learning environments like OpenAI Gym, you can modify an agent’s speed based on observations or a function:
    Example
    class CustomEnv(gym.Env):
    def step(self, action):
    speed = base_speed + np.sin(self.time_elapsed) * speed_multiplier
    self.agent.velocity = speed * action # Modify movement dynamics

  2. Robotics (ROS, Gazebo)
    For robotic agents (like in Gazebo/ROS):
    from geometry_msgs.msg import Twist

vel_msg = Twist()
vel_msg.linear.x = base_speed + math.sin(rospy.get_time()) * speed_multiplier
cmd_vel_publisher.publish(vel_msg)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants