Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObject is shaking - I want to fix this

Discussion in 'Scripting' started by RIw, Apr 15, 2015.

Thread Status:
Not open for further replies.
  1. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    Hello I've got a little problem with my Capsule Player.
    My Player is shaking on the Z axis - I don't know why,because everything should be ok.
    Player is shaking on the Z axis even if there is ZPosition freeze enabled.
    Is there any simple way to do this?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Are you by any chance really far from the origin? Is your Z at 10,000+?
     
  3. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    @GroZZleR
    No,My player is on the Z = 0 by the start of the game,When I hit W key,Z += 10 and with the S Key Z-=10;
    Z cannot be bigger than 30 and less than 0,if it is, Z = 20.
    The Player is shaking on the Z axis no matter on what position he is. :(
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    We'll need more information on this. There's several reasons this could be happening. It could be a physics conflict or something goofy in your code. I would first try checking any child objects you may have. If you have collider objects that are conflicting it could be causing this.
     
  5. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    @jtsmith1287 , yes I have child objects for my Player, but I'm using Physics.IgnoreCollision for that :p.
    Even if I delete these Child Objects, Player is still shaking :( .
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You still have to provide more information. Do you use rigidbodies or a characterController? What's the code that makes him move?
     
  7. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    Yes, I'm using rigidbodies,and here is my MovementCode:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyPlayerMovement : MonoBehaviour
    5. {
    6.     //first = 0; second = 3; max = 6; |z
    7.  
    8.     public float moveSpeed;
    9.  
    10.     [HideInInspector]
    11.     public bool canJump; //jeśli false to skok nie może być wykonany
    12.  
    13.     public static bool movingDown;
    14.     public static bool movingUp;
    15.  
    16.     public static bool movingRight = true;
    17.     public static bool movingLeft = true;
    18.  
    19.     private float yPos;
    20.     private float zPos;
    21.  
    22.  
    23.     public CollsionCorrector Coller;
    24.     public DownCollisionCorrector DownColler;
    25.  
    26.  
    27.  
    28.  
    29.  
    30.     void Start ()
    31.     {
    32.         //transform.position = new Vector3 (-5f,1f,0);
    33.         Coller = GameObject.FindGameObjectWithTag ("CollisionPoint1").GetComponent<CollsionCorrector> ();
    34.         DownColler = GameObject.FindGameObjectWithTag ("CollisionPoint2").GetComponent<DownCollisionCorrector> ();
    35.  
    36.         Physics.IgnoreCollision (GameObject.FindGameObjectWithTag("CollisionPoint1").GetComponent<BoxCollider>(),transform.collider);
    37.         Physics.IgnoreCollision (GameObject.FindGameObjectWithTag("CollisionPoint2").GetComponent<BoxCollider>(),transform.collider);
    38.  
    39.  
    40.     }
    41.  
    42.  
    43.     void Update () //TODO: Pozbądź się drgań - Praprzyczyna Drgania w osi Z
    44.     {
    45.         if(Input.GetButton("A") && movingLeft)
    46.         {
    47.             rigidbody.velocity = new Vector3(-40 * moveSpeed * 1f,rigidbody.velocity.y,0);
    48.         }
    49.         if(Input.GetButton("D") && movingRight)
    50.         {
    51.             rigidbody.velocity = new Vector3(40 * moveSpeed * 1f,rigidbody.velocity.y,0);
    52.         }
    53.      
    54.         if (!canJump)
    55.         {
    56.             rigidbody.AddForce(0,-10f,0);
    57.         }
    58.      
    59.         if (Input.GetButton ("Space") && canJump && !Coller.IsCollision) {
    60.                         canJump = false;
    61.                         //rigidbody.AddForce(0,250f,0);
    62.  
    63.                         rigidbody.velocity = new Vector3 (0, 11, 0);
    64.                 } else if (Input.GetButton ("Space") && canJump) {
    65.                         rigidbody.AddForce (0, -0.1f, 0);
    66.                 } else if (Input.GetButton ("Space") && !canJump) {
    67.                         rigidbody.AddForce (0, -0.1f, 0);
    68.                 }
    69.      
    70.         zPos = transform.position.z;
    71.  
    72.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    73.         Vector3 backward = transform.TransformDirection(Vector3.back);
    74.  
    75.         if(Input.GetButton("W") && !EditorMode.Editing)
    76.         {
    77.             if (zPos >= -0.2f && zPos <= 3f) //From first to second (zPos >= 1.9f && zPos <= 2.1f)
    78.             {
    79.                 if(!Physics.Raycast(transform.position,forward,2) && !Physics.Raycast(transform.position,forward,2))
    80.                 {
    81.  
    82.  
    83.                 movingUp = true;
    84.                 yPos = 3f;
    85.              
    86.                 }
    87.             }
    88.             else if(zPos >= 2.9f && zPos <= 6f && !Physics.Raycast(transform.position,forward,2)) // From second to max (zPos >= 4.9f && zPos <= 5.1f)
    89.             {
    90.                 if(!Physics.Raycast(transform.position,forward,2))
    91.                 {
    92.  
    93.  
    94.                 movingUp = true;
    95.                 yPos = 6f;
    96.                 }
    97.             }
    98.         }
    99.         if (Input.GetButton ("S") && !EditorMode.Editing)
    100.         {
    101.             if (zPos >= 2.9f && zPos <= 4.1f && !Physics.Raycast(transform.position,backward,2)) // second to first
    102.             {
    103.                 if(!Physics.Raycast(transform.position,backward,2))
    104.                 {
    105.  
    106.              
    107.                 movingDown = true;
    108.                 yPos = 0.1f;
    109.                 }
    110.             }
    111.             else if(zPos >= 5.9f && zPos <= 8.1f && !Physics.Raycast(transform.position,backward,2)) // max to second
    112.             {
    113.                 if(!Physics.Raycast(transform.position,backward,2))
    114.                 {
    115.              
    116.  
    117.          
    118.  
    119.                 movingDown = true;
    120.                 yPos = 3f;
    121.                 }
    122.             }
    123.         }
    124.         if (Input.GetKeyDown (KeyCode.E))
    125.         {
    126.  
    127.         }
    128.         Move_UP_or_Down (yPos);
    129.  
    130.     }
    131.     void Move_UP_or_Down(float target)
    132.     {
    133.         if (movingDown) {
    134.             if(zPos <= target)
    135.             {
    136.                 movingDown = false;
    137.             }
    138.             else
    139.             {
    140.                 transform.position = transform.position + transform.TransformDirection(-Vector3.forward) * moveSpeed;
    141.             }
    142.         }
    143.         if (movingUp) {
    144.             if(zPos >= target)
    145.             {
    146.                 movingUp = false;
    147.              
    148.             }
    149.             else
    150.             {
    151.                 transform.position = transform.position + transform.TransformDirection(Vector3.forward) *  moveSpeed;
    152.             }
    153.         }
    154.     }
    155.     void OnCollisionEnter(Collision coll)
    156.     {
    157.         if (Coller.IsCollision == false) {
    158.                         canJump = true;
    159.                 } else {
    160.             canJump = false;
    161.                 }
    162.         if (DownColler.IsCollision == false) {
    163.                         canJump = false;
    164.                 } else {
    165.             canJump = true;
    166.                 }
    167.     }
    168.     void OnCollisionStay(Collision coll)
    169.     {
    170.         if (Coller.IsCollision == false) {
    171.             canJump = true;
    172.         }else {
    173.             canJump = false;
    174.         }
    175.         if (DownColler.IsCollision == false) {
    176.             canJump = false;
    177.         } else {
    178.             canJump = true;
    179.         }
    180.         if (DownColler.IsCollision == true && Coller.IsCollision == true)
    181.         {
    182.             canJump = true;
    183.         }
    184.      
    185.     }
    186. }
    187.  
    Coller and DownColler are the Empty Gameobjects which are checking that is Player staying on the Other GameObject or is he stuck.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    You are accessing the physics (via .rigidbody) inside an Update() function.

    Always used FixedUpdate() for physics otherwise you will see this exact jittering.

    To clarify, rename the Update() method in any script that wrangles with physics to instead be FixedUpdate(). I will leave it to you to read in the documentation about any other subtle consequences this may have.
     
  9. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Also, to add to Kurt, I don't see anywhere where you are zeroing your rigidbody velocity. It is always a good idea to set it equal to Vector3.zero if you know you shouldn't be moving.
     
  10. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    ibrahimonatyilmaz likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Is there a camera following this object by some kind of script? If so, if it uses Update() then it also needs to use FixedUpdate().
     
    betocanal27 likes this.
  12. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    @Kurt Dekker , I've used FixedUpdate in the Camera following but also there is no difference,I see this Player's shaking on the Scene, so I think this is not the Camera's fault.
     
  13. Juandapp

    Juandapp

    Joined:
    May 11, 2016
    Posts:
    53
    @kurt Dekker I have the same problem with pixel art objects, did you manage to solve it?
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    This is a) an old thread, and b) a very generic problem with about a hundred possible solutions depending on the details of the situation. Please make a new post with more information (including your movement script code).
     
    Kurt-Dekker likes this.
  15. rahulxmarty

    rahulxmarty

    Joined:
    Jan 23, 2022
    Posts:
    1
    I have the exact same issue I need instant help! Is there anyone ?
     
    Noor23 likes this.
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    When you need instant help, don't reply to a three-year-old thread. It's also against forum rules.

    START YOUR OWN NEW THREAD... It's FREE.

    Here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,315
    Locking this necro magnet. As stated above, please put a little effort into creating your own threads and describing your own problem.
     
Thread Status:
Not open for further replies.