Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem trembling asset when moving

Discussion in 'Editor & General Support' started by Bervt, Jun 26, 2020.

  1. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    Hello

    Beginner here

    i try to make a "roll a ball "game and i have this problem
    (xbox controler )
    i don't know what is causing this problem
    i supose it is my code , but for what i have write it is working for me (mean it do somewhat the job , 3 person with orbit and folowup movement(camera and ball ) and physics )
    (try cinemachine , but do not work at all , the camera move randomly when i move )
    if you can help thank you

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public float maxSpeed;
    5.     public Transform cam;
    6.     public int vitesseCameraRotation;
    7.     public float distToGround = 0.85f;
    8.     public LayerMask LayerMask;
    9.    
    10.     private Rigidbody rb;
    11.     private bool onGround;
    12.  
    13.    
    14.     void Start ()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.        
    18.     }
    19.     void FixedUpdate ()
    20.     {
    21.    
    22.  
    23.         Debug.Log (onGround);
    24.        
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.         //camera direction influence
    28.         Vector3 camF = cam.forward;
    29.         Vector3 camR = cam.right;
    30.  
    31.         camF.y = 0;
    32.         camR.y = 0;
    33.         camF = camF.normalized;
    34.         camR = camR.normalized;
    35.  
    36.         Vector3 movement = (camF*moveVertical + camR*moveHorizontal)*Time.deltaTime*vitesseCameraRotation;
    37.         // ground apply addforce
    38.         if (onGround)
    39.         {
    40.            rb.AddForce (movement * speed);
    41.         }
    42.        
    43.         // limit speed
    44.         if(rb.velocity.magnitude > maxSpeed)
    45.         {
    46.             rb.velocity = rb.velocity.normalized * maxSpeed;
    47.         }
    48.  
    49.         // ground detection
    50.         RaycastHit hit;
    51.         if(Physics.SphereCast (transform.position,distToGround, Vector3.down, out hit, distToGround+1.0f,LayerMask,QueryTriggerInteraction.UseGlobal))
    52.         {
    53.             onGround = true;
    54.         }
    55.         else
    56.         {
    57.             onGround = false;
    58.         }
    59.    
    60.     }
    61.  
    62.    
    63.  
    64.  
    65. }
    Code (CSharp):
    1. public class CameraControler : MonoBehaviour
    2. {
    3.     public Transform PlayerTransform;
    4.  
    5.     public Vector3 offset;
    6.     public float yoffset;
    7.     public float xoffset;
    8.     public float RotationsSpeed = 5.0f;
    9.  
    10.      void Start () {
    11.         offset = new Vector3(PlayerTransform.position.x, PlayerTransform.position.y + yoffset, PlayerTransform.position.z + xoffset);
    12.     }
    13.  
    14.  
    15.  
    16.     void LateUpdate () {
    17.  
    18.         offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up) * offset;
    19.           transform.position = PlayerTransform.position + offset;
    20.           transform.LookAt(PlayerTransform.position);
    21.  
    22.        
    23.  
    24.     }
    25.  
    26.    
    27.  
    28. }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Can you be more specific about the problem? I don't know if it's the first few seconds of the video, where the texture seems to be moving on the sphere. Otherwise, I can't really see any trembling while the ball's moving, but that might be due to the video compression.

    Do you have a special shader on this material that's causing it to move?
     
  3. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    hi , Thank you for your answer

    it is not the ball that have a problem trembling
    look at the duck or at the pro builder asset
    it seem to be like a kind of frame rate problem (i do not see it on the ball )
    (the ball no special shader , diffuse , normal , specular , emission )
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Sorry, it's really difficult to tell from the video what the problem is. I see some artifacts on the duck and the level, but I can't tell that apart from basic video compression noise in the video itself.

    If you think there's a frame rate issue, though, you should use the profiler to determine whether some frames are taking extra long to render.
     
  5. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    Thank you ,
    i think i will try to restart from zero
    it is not the frame rate , seem constant
    it seem it only do it when i use mouseX and a direction , maybe it is here that i must look ,
    i m going to try making (hoping to :D ) cleaner code
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Well, I think you could get some help if you very clearly explain the problem. I really just can tell exactly what the issue is in your video. Maybe a shorter video, just a couple of seconds, showing the issue? Make sure the quality if high so that it doesn't have too many video artifacts.
     
  7. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    Ask some friend , if they see what is the problem and they see it ,I think what you think of video artefact is my problem .
    i have remake a video with i hope better quality

    on the image we can see that the duck jerks as if the image frequency is not constant each time I turn the camera and move at the same time, because when I release the camera the shaking stops. I hope my problem is a little clearer ^^'.
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Okay, thanks for clarifying. So the issue is that when you're rotating with the mouse, it's not as smooth as you'd like. It sort of makes lots of small jumps every few frames instead of turn smoothly.

    First, it would be good to know whether this only happens if you're moving, or if it also happens when you move the mouse while the ball is standing still. You don't have that in any of your videos, so it's hard to say.

    I'm not sure what's causing the issue. But since you're moving the ball in FixedUpdate, and called LookAt in LateUpdate, things will only update as quickly as FixedUpdate is called. Have you tried turning on Interpolation on your player's rigidbody? Try setting it to "Interpolate", and see if that helps at all. But also let us know whether things rotate smoothly if the player isn't moving, just so we definitely know it's related to the rigidbody movement.
     
  9. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    I have try to fix it by making all in FixedUpdate
    and it fix the problem ^^ (learn something about update , Thanks !)

    Camera was rotating smoothly
    I try Interpolate and the ball this time was stutering like crazy
     
  10. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Hmm. Doing everything in FixedUpdate isn't ideal, so you probably still have some work to do. FixedUpdate is generally only for performing physics updates. Adjusting the camera rotation in FixedUpdate just seems a bit wrong. But I can't explain why this helps in your case.
     
  11. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Hi and welcome to using Unity :)

    Your code looks rather clean to me already and you seem to be doing a lot of things right already. You've used the right update methods and already ensured that the ball movement is frame rate independent by multiplying it with Time.deltaTime.

    However it looks like your LateUpdate code isn't frame rate independent, i.e. the rotation speed is the same, no matter the frame rate. So if Time.deltaTime changes one frame to the next, you'll get different speeds of rotation from one frame to the next. (And there's nearly always some minor changes in that value in the Update and LateUpdate cycles.)

    Not sure if that's the only problem here but you might want to try to go back to using LateUpdate for the camera and writing it like this instead:
    Code (CSharp):
    1. void LateUpdate () {
    2.         offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * RotationsSpeed * Time.deltaTime, Vector3.up) * offset;
    3.           transform.position = PlayerTransform.position + offset;
    4.           transform.LookAt(PlayerTransform.position);
    5.      
    6.     }
    7.  
    You'll have to adjust the rotation speed which now would have a unit of "degrees of rotation per second", so the value would need to be higher than it is now (e.g. if you are currently running it at a fixed 60 FPS, it'd need to be 60x larger for the same speed).
     
  12. Bervt

    Bervt

    Joined:
    Jun 19, 2020
    Posts:
    13
    Hi , thank you for your answer ,
    i have try but it seem to do the same problem

    i think i know why it do that , rotation of the camera is on camera script , and it influence the vector of the player script if it is not at the same update there is a conflict the game is for me trying to put the same frame at 2 different time .

    i m putting this idea in a corner in my head , will propably come back rewrite this later if this is too much of a problem , i m going to work on other thing , gain more experience in coding ^^ .

    i have remove Time.deltaTime for the player script too , because like it was it made the player accelerate no stop ,
    it was the cause of my trying to limit the speed with rb.velocity , who cause a problem too because it limit speed even when gravity is affecting the ball (constant speed ) :D the joy of games physics