0
\$\begingroup\$

As it says, .MovePosition() won't move my character. I know other ways to move but I want .MovePosition() in particular as I think it will solve the camera's jittery movement when the player pushes through a collision.

This script works on a kinematic rigidbody but not on a dynamic one.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    Rigidbody2D rbody;
    Animator anim;
    public float speed = 0;
    SpriteRenderer sr;

    void Start () {
        rbody = GetComponent<Rigidbody2D> ();
        anim = GetComponent<Animator> ();
        sr = GetComponent<SpriteRenderer>();
    }

    void Update(){
        sr.sortingOrder = ((int)Camera.main.WorldToScreenPoint (sr.bounds.min).y) * -1;
        Vector2 movementVector = new Vector2 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        if (movementVector != Vector2.zero) {
            anim.SetBool ("isWalking", true);
            anim.SetFloat ("inputX", movementVector.x);
            anim.SetFloat ("inputY", movementVector.y);
        } else {
            anim.SetBool ("isWalking", false);
        }
        Vector2 newPos = rbody.position + movementVector *Time.fixedDeltaTime;
        rbody.MovePosition(newPos);
        //rbody.position = Vector2.MoveTowards(rbody.position, newPos, Time.deltaTime*speed);
    }
}

FYI I'm on Unity 5.6 if that helps.

\$\endgroup\$
1
  • \$\begingroup\$ The docs do mention "if the colliders produce a collision then it will affect the rigidbody movement and potentially stop it from reaching the specified position during the next physics update" - have you verified that your body in its starting position isn't adjacent/embedded in any colliders that could be stopping it? \$\endgroup\$ – DMGregory Jul 11 '20 at 13:02
-1
\$\begingroup\$

You are right, you can not use MovePosition with non kinematic rigidbody.

From Unity Rigidbody.MovePosition documenttion:

Use Rigidbody.MovePosition to move a Rigidbody, complying with the Rigidbody's interpolation setting.

(...)

Set Rigidbody.position instead, if you want to teleport a rigidbody from one position to another, with no intermediate positions being rendered.

(...)

If the rigidbody has isKinematic set false then it works differently. It works like transform.position=newPosition and teleports the object (rather than a smooth transition).

TLDR;

MovePosition is for kinematic rigidbodies as those are not affected by phisics you can just move them where you want. Non-kinematic rigidbodies are affected by physics, thus should be moved with AddForce method. It is either one or the other.

\$\endgroup\$
0
\$\begingroup\$

@IndieForger answer above is correct but you CAN use MovePosition with non kinematic rigidbody as well even though docs says it's mostly for kinematic. JUST MAKE SURE 'Simulated' is checked ON in Rigidbody component and if U don't want gravity set it to 0. Then it will work as intended.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The docs do not say it's "mostly" for kinematic. In fact, what they do say is that for kinematic bodies, MovePosition acts like body.position directly. In that sense, the truth is the opposite of IndieForger's answer: a substantial amount of what MovePosition does is only for dynamic, *non-*kinematic bodies. They're also referencing the docs for the 3D version, not the 2D version as used in the question, which goes into more detail on other things MovePosition does for dynamic bodies. \$\endgroup\$ – DMGregory Jul 11 '20 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.