Search Unity

Noob with Collision Question - Jittery

Discussion in 'Getting Started' started by SFDCIVimyProject, Oct 22, 2015.

  1. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    Hi,

    I have a simple question but I'm having a hard time understanding what the solution is.
    I create very simple player and platform game objects. When the two collide and I move the player, there is a jitter/bounce that happens as the player gets sucked into the platform and gets shot out the other side.
    Both objects have box colliders. The player has a Rigidbody (with gravity turned on).

    Here is how I've created the game.
    Platform
    I Create a New 3D Object => New => Cube
    Change the scale so that it looks like a platform

    Player
    I Create a New 3D Object => New => Cube
    Add a Rigidbody (leaving all settings to default)
    Create a Script:
    float targetSpeed = Input.GetAxisRaw ("Horizontal") * speed; // speed is a public float variable
    rb.AddForce (targetSpeed, 0.0f, rb.velocity.y); // rb is the Rigidbody initialized in the Start function


    I have no idea what can cause the collisions between the two objects to gradually get sucked into one another?

    Any help is greatly appreciated!
     
  2. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    I have tested your script, See here;
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SFDCIVimyProject : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     private Rigidbody rb;
    8.     // Use this for initialization
    9.     void Start () {
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         float TargetSpeed = Input.GetAxisRaw("Horizontal") * speed;
    16.         rb.AddForce (TargetSpeed, 0.0f, rb.velocity.y);
    17.     }
    18. }
    With this code, The Player (Cube) was rotating. Here is the fix

    Step 1 : Add a ground
      • You can use scaled cube or a plane
      • Do not change any other
    Step 2 : Add the player
      • Add new cube
      • Set position Above the ground
    Step 3 : Character Controller
      • Add a Character Controller to the player object
      • Use Add Component > Physics > Character Controller
    Step 4 : Add Controller Script
      • Add New Script
      • Code (CSharp):
        1. using UnityEngine;
        2. using System.Collections;
        3.  
        4. public class SFDCIVimyProject : MonoBehaviour {
        5.  
        6.     public float speed; // From your script
        7.     private CharacterController charactor; // Changed for fixing
        8.  
        9.     // Use this for initialization
        10.     void Start () {
        11.  
        12.         charactor = GetComponent<CharacterController>(); // Changed for fixing
        13.     }
        14.    
        15.     // Update is called once per frame
        16.  
        17.     void Update () {
        18.  
        19.         float targetSpeed = Input.GetAxisRaw ("Horizontal") * speed; // From your script
        20.         charactor.SimpleMove(Vector3.right * targetSpeed); // Changed for fixing
        21.     }
        22. }

        Step 5 : Endings
      • Set the speed to 5 - 10 in Inspector
      • Be sure your are not changed the input settings
      • Use the * Time.deltaTime if you need smoother movement
      • if you using Time.deltaTime, Increase the value of speed
    I think i have fixed your problem
     
    Last edited: Oct 22, 2015
  3. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    Thanks, I appreciate your help and that did work.

    Why would the rotation of the original player (Cube) cause this odd behaviour in the first place?

    Does adding the Character Controller fix this issue because it's adding some sort of 'skin' in between the objects that are colliding?

    Thanks again
     
  4. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    sorry if i am too late.

    A cube cannot move freely with it's sharp edges. Rigidbody force applied to one side of the
    Object. That Cause non realistic behaviors or motions.

    Character controller can fix it. It is not use a force to the object. That moves it.
    But you must change the height and radius of Character controller.

    To fix the collision bug. Read the offline documentation came with unity here
    <Unity install directory>/Editor/Data/Documentation/en/Manual/class-CharacterController.html
    This is also Available Online