Vanilla_Fury

Untitled

Dec 26th, 2021
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. void APawnWithPlanetHorizon::Tick(float DeltaTime)
  2. {
  3.     Super::Tick(DeltaTime);
  4.  
  5.     if (HasAuthority())
  6.     {
  7.         FVector posToPlanet = GetActorLocation() - LocationOfCenterOfGravity;
  8.         posToPlanet.Normalize();
  9.         FRotator rot = UKismetMathLibrary::MakeRotFromZX(posToPlanet, GetActorForwardVector());
  10.         SetActorRotation(rot);
  11.         SetActorLocation((posToPlanet * RadiusOfPlanet) + LocationOfCenterOfGravity);
  12.  
  13.         VisibleMeshBody->SetPhysicsAngularVelocityInDegrees(FVector(0., 0., 0.));
  14.         VisibleMeshHead->SetPhysicsAngularVelocityInDegrees(FVector(0., 0., 0.));
  15.  
  16.         VisibleMeshBody->SetPhysicsLinearVelocity(FVector::VectorPlaneProject(VisibleMeshBody->GetPhysicsLinearVelocity(), posToPlanet).GetClampedToMaxSize(maxSpeedOfPawn));
  17.     }
  18.     else if (!IsLocallyControlled())
  19.     {
  20.         VisibleMeshHead->SetRelativeRotation(FRotator(WantedByServerRotOfCharacter.Pitch, 0, 0));
  21.         VisibleMeshHead->SetPhysicsAngularVelocityInDegrees(FVector(0., 0., 0.));
  22.         VisibleMeshBody->SetRelativeRotation(FRotator(0, WantedByServerRotOfCharacter.Yaw, 0));
  23.         VisibleMeshBody->SetPhysicsAngularVelocityInDegrees(FVector(0., 0., 0.));
  24.     }
  25. }
  26.  
  27. void APawnWithPlanetHorizon::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  28. {
  29.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  30.  
  31.     InputComponent->BindAction("MouseLeftClick", IE_Pressed, this, &APawnWithPlanetHorizon::MouseLeftClick);
  32.  
  33.     InputComponent->BindAxis("MoveForward", this, &APawnWithPlanetHorizon::MoveForwardAxis);
  34.     InputComponent->BindAxis("MoveRight", this, &APawnWithPlanetHorizon::MoveRightAxis);
  35.     InputComponent->BindAxis("RotateX", this, &APawnWithPlanetHorizon::RotateXAxis);
  36.     InputComponent->BindAxis("RotateY", this, &APawnWithPlanetHorizon::RotateYAxis);
  37. }
  38.  
  39. void APawnWithPlanetHorizon::MoveRightAxis(float AxisValue)
  40. {
  41.     ServerRPC_MoveRightAxis(AxisValue);
  42. }
  43.  
  44. void APawnWithPlanetHorizon::MoveForwardAxis(float AxisValue)
  45. {
  46.     ServerRPC_MoveForwardAxis(AxisValue);
  47. }
  48.  
  49. void APawnWithPlanetHorizon::RotateXAxis(float AxisValue)
  50. {
  51.     ServerRPC_RotateXAxis(VisibleMeshBody->GetRelativeRotation().Yaw);
  52.  
  53.     FRotator rot = VisibleMeshBody->GetRelativeRotation() + FRotator(0, AxisValue, 0);
  54.  
  55.     VisibleMeshBody->SetRelativeRotation(rot);
  56.     ServerRPC_RotateYAxis(rot.Yaw);
  57. }
  58.  
  59. void APawnWithPlanetHorizon::RotateYAxis(float AxisValue)
  60. {
  61.     FRotator rot = VisibleMeshHead->GetRelativeRotation() + FRotator(AxisValue, 0, 0);
  62.  
  63.     if (rot.Pitch > CameraVerticalAngleMax)
  64.     {
  65.         rot.Pitch = CameraVerticalAngleMax;
  66.     }
  67.     else if (rot.Pitch < CameraVerticalAngleMin)
  68.     {
  69.         rot.Pitch = CameraVerticalAngleMin;
  70.     }
  71.     VisibleMeshHead->SetRelativeRotation(rot);
  72.     ServerRPC_RotateYAxis(rot.Pitch);
  73. }
  74.  
  75. void APawnWithPlanetHorizon::ServerRPC_MoveRightAxis_Implementation(float AxisValue)
  76. {
  77.     if (AxisValue)
  78.     {
  79.         FVector spdToAddTemp = VisibleMeshBody->GetRightVector() * AxisValue;
  80.         float coof = 1;
  81.         if (FVector::DotProduct(MeshMass->GetPhysicsLinearVelocity(), spdToAddTemp) < -3)
  82.         {
  83.             coof = 3;
  84.         }
  85.        
  86.         MeshMass->SetPhysicsLinearVelocity(spdToAddTemp * 3 * coof, true);
  87.     }
  88.     else
  89.     {
  90.         FVector rightSpd = MeshMass->GetPhysicsLinearVelocity().ProjectOnToNormal(VisibleMeshBody->GetRightVector());
  91.         MeshMass->SetPhysicsLinearVelocity(-rightSpd.GetClampedToMaxSize(2), true);
  92.     }
  93. }
  94.  
  95. void APawnWithPlanetHorizon::ServerRPC_MoveForwardAxis_Implementation(float AxisValue)
  96. {
  97.     if (AxisValue)
  98.     {
  99.         FVector spdToAddTemp = VisibleMeshBody->GetForwardVector() * AxisValue;
  100.         float coof = 1;
  101.         if (FVector::DotProduct(MeshMass->GetPhysicsLinearVelocity(), spdToAddTemp) < -3)
  102.         {
  103.             coof = 3;
  104.         }
  105.         MeshMass->SetPhysicsLinearVelocity(spdToAddTemp * 3 * coof, true);
  106.     }
  107.     else
  108.     {
  109.         FVector fwdSpd = MeshMass->GetPhysicsLinearVelocity().ProjectOnToNormal(VisibleMeshBody->GetForwardVector());
  110.         MeshMass->SetPhysicsLinearVelocity(- fwdSpd.GetClampedToMaxSize(2), true);
  111.     }
  112. }
  113.  
  114. void APawnWithPlanetHorizon::ServerRPC_RotateXAxis_Implementation(float WantedAngleCameraX)
  115. {
  116.     VisibleMeshBody->SetRelativeRotation(FRotator(0, WantedAngleCameraX, 0));
  117.     WantedByServerRotOfCharacter.Yaw = WantedAngleCameraX;
  118. }
  119.  
  120. void APawnWithPlanetHorizon::ServerRPC_RotateYAxis_Implementation(float WantedAngleCameraY)
  121. {
  122.     VisibleMeshHead->SetRelativeRotation(FRotator(WantedAngleCameraY, 0, 0));
  123.     WantedByServerRotOfCharacter.Pitch = WantedAngleCameraY;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment