Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

My player is jerky when use Rigidbody.Moveposition/.velocity/.addforce

Please help! I try Rigidbody.Moveposition/ Rigidbody.velocity/ Rigidbody.addforce also character controller all of them not smooth like transform.translate. I also changed to fixed update but still jerky when move can someone help me with this problem much much appreciate.

Here my code

using UnityEngine;
using System.Collections;

public class ControlButtonMovement : MonoBehaviour {

    private Animator playerAnimator;
    private Rigidbody playerRigidbody;
    private float movementSpeed = 200.1f;
    private float turningSpeed = 20f;

    private bool check = false;
    private string name;

    private Vector3 vec;

    // Use this for initialization
    void Start () {

        // Gather components from the Player's Item GameObject
        playerAnimator = GetComponent<Animator> ();
        playerRigidbody = GetComponent<Rigidbody> ();

    }

    //Function use to check if Up button is pressed by parameter.
    public void PlayerWalkUp (int valueU) {
        if (valueU == 1) {

            transform.rotation = Quaternion.Euler (0, 0, 0);
            check = true;
            name = "up";

        } else {

            check = false;

        }
    }

    //Function use to check if Down button is pressed by parameter.
    public void PlayerWalkDown (int valueD) {
        if (valueD == 2) {

            transform.rotation = Quaternion.Euler (0, 180, 0);
            check = true;
            name = "down";
        } else {

            check = false;

        }
    }

    //Function use to check if Left button is pressed by parameter.
    public void PlayerWalkLeft (int valueL) {
        if (valueL == 3) {

            transform.rotation = Quaternion.Euler (0, -90, 0);
            check = true;
            name = "left";
        } else {

            check = false;

        }
    }

    //Function use to check if Right button is pressed by parameter.
    public void PlayerWalkRight (int valueR) {
        if (valueR == 4) {

            transform.rotation = Quaternion.Euler (0, 90, 0);
            check = true;
            name = "right";
        } else {

            check = false;

        }
    }

    void FixedUpdate() {

        if (check == true) {
            playerAnimator.SetFloat ("Speed", 1.0f);
            //transform.Translate (Vector3.forward * movementSpeed * Time.deltaTime);
            //playerRigidbody.AddForce (transform.forward * movementSpeed);
            //playerRigidbody.MovePosition (transform.position + transform.forward * Time.deltaTime);

//          if (name == "up") {
//              vec = new Vector3 (0, 0, 1) * movementSpeed * Time.deltaTime;
//
//          } else if (name == "down") 
//          {
//              vec = new Vector3 (0, 0, -1) * movementSpeed * Time.deltaTime;
//
//          } else if (name == "left") 
//          {
//              vec = new Vector3 (-1, 0, 0) * movementSpeed * Time.deltaTime;
//
//          } else if (name == "right") 
//          {
//              vec = new Vector3 (1, 0, 0) * movementSpeed * Time.deltaTime;
//
//          }
//          playerRigidbody.velocity = vec;

            playerRigidbody.isKinematic = false;
            //print ("isKinematic = false");

        } else if (check == false) {

            playerAnimator.SetFloat("Speed", 0.0f);
            playerRigidbody.isKinematic = true;
            //print ("isKinematic = ture");
        }

    }


}