Search Unity

Clamp Y with Rigidbody character using addforce (drone)

Discussion in 'Scripting' started by donovand4, Jul 11, 2019.

  1. donovand4

    donovand4

    Joined:
    May 8, 2013
    Posts:
    3
    Hi All,
    Im struggling with clamping my character to the camera bounds. For some reason when the character reaches the clamp it begins to jitter. I have tried different force methods, clamp positions vs velocity but I really do need some help as I am not a very experienced coder.

    The project is a side scroller using 3d meshes and the character needs to feel like a drone which is why I am using addforce instead of position.

    transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -2f, 5.1f), transform.position.z);

    //input region for managing the movement of the character
    //Upwards movement
    if (Input.GetAxis("Vertical") > 0.0f)
    {
    //rigidbodyCharacter.AddForce(0, Upwardthrust, 0, ForceMode.Acceleration);
    rigidbodyCharacter.AddForce(0, Upwardthrust, 0, ForceMode.Force);
    }

    Any ideas?
     
    Last edited: Jul 12, 2019
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    44
    Do you use Update() or FixedUpdate()?
     
  3. donovand4

    donovand4

    Joined:
    May 8, 2013
    Posts:
    3
    FixedUpdate()
     
  4. donovand4

    donovand4

    Joined:
    May 8, 2013
    Posts:
    3
    Fixed it by removing the forcemode and reducing the parameters to the minimum required.

    rigidbodyCharacter.AddForce(Vector3.up * Upwardthrust);