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
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to set character rotations by Input?

Discussion in 'Project Tiny' started by Moopra, Oct 9, 2020.

  1. Moopra

    Moopra

    Joined:
    Jun 4, 2015
    Posts:
    30
    I tried to reproduce the old Unity behavior that my character will rotate according to user input.
    However I can't made it in Tiny.
    I tried to modify the rotation script that come with the windmill sample (Tiny3D).
    But my character rotation alway set to (0,0,0) when there are no user input.
    And also that windmill script used total elapsed time to calculate the new rotation.
    But I want the character to continue rotate from where it was set last time.
    Thanks for the help guys.


    Code (CSharp):
    1.   protected override void OnUpdate()
    2.         {
    3.             float time = (float)Time.DeltaTime;
    4.             Entities.ForEach((ref PlayerRotator player, ref PlayerInput playerInput,ref Rotation rotation ) =>
    5.             {
    6.  
    7.                     quaternion qx = quaternion.RotateX(0);
    8.                     quaternion qy = quaternion.RotateY(time * player.Speed * playerInput.InputAxis.x);
    9.                     quaternion qz = quaternion.RotateZ(0);
    10.                     rotation.Value = math.normalize(math.mul(qz, math.mul(qy, qx)));
    11.  
    12.             }).ScheduleParallel();
    13.         }
    This video is the showcase of what I really want. (It was made in ECS)

     
    Last edited: Oct 9, 2020
  2. Moopra

    Moopra

    Joined:
    Jun 4, 2015
    Posts:
    30
    I managed to get it works guys!

    I ended up have to add a component to store the quaternion data to my object.

    Code (CSharp):
    1.  [GenerateAuthoringComponent]
    2.     public struct RotationValue : IComponentData
    3.     {
    4.         public quaternion q;
    5.     }
    then I updated this value in the system code and use it to set rotation.

    Code (CSharp):
    1.     Entities.ForEach((ref CameraRotator player, ref CameraInput playerInput,ref Rotation rotation , ref RotationValue value) =>
    2.             {
    3.  
    4.                     quaternion q = new quaternion(0, value.q.value.y + (playerInput.InputAxis.x * player.Speed * time), 0 ,0);
    5.                     value.q = q;
    6.                     rotation.Value = quaternion.RotateY(value.q.value.y);
    7.  
    8.             }).ScheduleParallel();
    Hope this help if you also face this issue.
     
    Last edited: Oct 10, 2020
    Ted_Wikman and AbdulAlgharbi like this.
  3. JumpingGuy

    JumpingGuy

    Joined:
    Jan 2, 2016
    Posts:
    69
    Hi @Moopra
    Can I ask how you attached this to your character?

    If I understand this correctly, I make a new component script for the first code snippet? Is this right and what do I name it?

    And then where does this go?

    I'm a newbie to coding but really keen on the potential that Project Tiny has for the web. I'm trying to make a smooth character controller - the only sample I can find in Project Tiny is in the Physics sample but it jumps rotation harshly with no transition. Any help you can offer would be appreciated. Thanks.