Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Tiny Physics Player Controller modification

Discussion in 'Project Tiny' started by Scuro247, Feb 24, 2021.

  1. Scuro247

    Scuro247

    Joined:
    Dec 4, 2018
    Posts:
    3
    Hello everyone,

    I recently started working with ECS and Tiny. I love Project Tiny and I really appreciate it being open source! Thanks a lot!

    Currently, I am trying to modify the Tiny Physics player controller. I want to get something like this third-person controller [1] but in a less advanced way. It is very important for me to do everything in pure ECS, to get some experience, and have good performing code.

    The first milestone I want to reach is to make the camera follow the player and move around it by moving the mouse. Therefore I added the following components:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Tiny.Input;
    3. using Unity.Tiny.Rendering;
    4. using Unity.Mathematics;
    5. using Unity.Transforms;
    6.  
    7. namespace TinyPhysics.Systems
    8. {
    9.     [UpdateBefore(typeof(CameraMovementSystem))]
    10.     public class RotateWithMouseSystem: SystemBase
    11.     {
    12.         protected override void OnUpdate()
    13.         {
    14.              InputSystem input = World.GetExistingSystem<InputSystem>();
    15.             ScreenToWorld s2w = World.GetExistingSystem<ScreenToWorld>();
    16.             float2 inputPos = input.GetInputPosition();
    17.             float3 cursorPos = s2w.InputPosToWorldSpacePos(inputPos, 4.0f);
    18.  
    19.             // calculate look direcction vector
    20.             Entities.ForEach((ref CameraMoveable moveable, ref Translation position, ref LocalToWorld localToWorld) =>
    21.             {
    22.                 float3 lookDirection = cursorPos + position.Value;
    23.                 quaternion lookRotation = quaternion.LookRotation(lookDirection, localToWorld.Up);
    24.                 moveable.rotation = new quaternion(lookRotation.value.x, lookRotation.value.y, 0, lookRotation.value.w);
    25.             }).ScheduleParallel();
    26.         }
    27.     }
    28. }
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3.  
    4. namespace TinyPhysics
    5. {
    6.     [GenerateAuthoringComponent]
    7.     public struct CameraMoveable : IComponentData
    8.     {
    9.         public quaternion rotation;
    10.         public float3 positionOffset;
    11.         public float3 position;
    12.         public Entity entityToFollow;
    13.     }
    14. }
    15.  
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Physics;
    4. using Unity.Physics.Extensions;
    5. using Unity.Transforms;
    6.  
    7. namespace TinyPhysics.Systems
    8. {
    9.     public class CameraMovementSystem : SystemBase
    10.     {
    11.         // EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    12.         protected override void OnUpdate()
    13.         {
    14.             float deltaTime = Time.DeltaTime;
    15.  
    16.             Entities.ForEach((ref CameraMoveable moveable, ref Rotation rotation, ref Translation position, ref LocalToWorld localToWorld) =>
    17.             {
    18.                 // rotate camera
    19.                 if (moveable.rotation.value.y != 0)
    20.                 {
    21.                     // rotation to world: localToWorld.Rotation
    22.                     quaternion rotation_world = localToWorld.Rotation;
    23.                     rotation.Value = moveable.rotation;
    24.                 }
    25.  
    26.                 //// move camera
    27.                 //if (true)
    28.                 //{
    29.                 //    Translation entPos = manager.GetComponentData<Translation>(moveable.entityToFollow);
    30.                 //    position.Value = entPos.Value + moveable.positionOffset;
    31.                 //}
    32.  
    33.             }).ScheduleParallel();
    34.         }
    35.     }
    36. }
    37.  
    As you can see my code has not been finished, yet. ;)

    One of the problems I have is that I am not able to calculate a proper lookDirection for my camera. Answer #4 in this thread [2] stated how to calculate the position of the cursor in the world space. I thought subtracting it from the current position of the camera should give me a direction where the camera should look. But this doesn't work at all. I think I got something wrong here. May someone enlighten me on this matter?

    Another problem I am having is that I cant make the camera follow the player entity. This video tutorial [3] illustrates how to set up a camera to follow an entity. The problem I am having here is that I can not access the position of the entity to follow. I wanted to use
    GetComponentData<Translation>
    to get the position but this only works with .WithoutBurst().Run() instead of ScheduleParallel() in the ForEach loop. Is there a way to get the current position of an entity in a more performant way? Or is there another solution to solve this problem?

    As you can see I am new to ECS and I really appreciate any help! Thanks in advance!

    Cheers
    scuro

    [1] https://assetstore.unity.com/packag...person-controller-basic-locomotion-free-82048
    [2] https://forum.unity.com/threads/cant-get-camera-point-to-ray-in-tiny.917249/#post-6049001
    [3] youtube.com/watch?v=JFv49-0vy_
     
  2. AbdulAlgharbi

    AbdulAlgharbi

    Unity Technologies

    Joined:
    Jul 27, 2018
    Posts:
    319
  3. Scuro247

    Scuro247

    Joined:
    Dec 4, 2018
    Posts:
    3
    Hey Abdul!
    Thank you for your quick reply! Exactly what I was looking for. I will keep you updated on how it worked out.
     
    AbdulAlgharbi likes this.