MovingPlatform behaves the same on client and server

Hi,

My code looks almost identical (seems like CoPilot is pulling code suggestions from the repo, to be honest). But I see the object moving on both client and server for some reason.

// Fill out your copyright notice in the Description page of Project Settings.


#include "MovingPlatform.h"

AMovingPlatform::AMovingPlatform() 
{
	PrimaryActorTick.bCanEverTick = true;

	SetMobility(EComponentMobility::Movable);
}

void AMovingPlatform::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (HasAuthority())
	{
		FVector Location = GetActorLocation();
		Location += FVector(Speed * DeltaTime, 0, 0);
		SetActorLocation(Location);
	}
}

void AMovingPlatform::BeginPlay()
{
	Super::BeginPlay();

	if (HasAuthority())
	{
		SetReplicates(true);
		SetReplicateMovement(true);
	}
}

This code basically says “If I’m the server, replicate this object’s movement to all of the clients.” Then every tick, it’s saying “If I’m the server, calculate a location and set the actor to that location”. Because the movement is being replicated, it will be moving on the server and clients.

2 Likes

yes, I’ve realized that the code actually skips into the next lesson of the section :rofl:
Thanks for the response.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms