- Home /
rigidbody2D.MovePosition() Can't Found
As per my title suggest, I can't found rigidBody2D.MovePosition() method.
I have seen this method used in Catch Game.
Although I have downloaded latest Unity also. But I have same result. So what is the problem here? Please help me to solve this.
Strange. I suspect he is using a beta of a future release. You can get approximately the same behavior by changing that line to:
transform.position = targetPosition;
transform.position = targetPosition;
Worked for me. Thanks!
Answer by Kiwasi · May 20, 2014 at 05:02 AM
There is no RigidBody2D.MovePosition() method. Link to docs
But it would be very useful to have a method like this that avoided the problems of Transform.Translate() when applied to a RigidBody.
I hope in future version we get this method available.
To clarify, there is a RigidBody.$$anonymous$$ovePosition() method
There is not a RigidBody2D.$$anonymous$$ovePosition() method.
There is a Rigidbody2D.$$anonymous$$ovePosition() now http://docs.unity3d.com/ScriptReference/Rigidbody2D.$$anonymous$$ovePosition.html
Please add your comment as answer so that I can make it correct to help other members.
Answer by bhison · Jun 26, 2015 at 03:05 PM
The advice on this thread is now out of date. MovePosition is a non-beta function in the standard release. (documentation)
Using this function is different to how it is explained in the video; just following instructions will lead to compiler errors. Following the guidance in documentation I have made the following code which operates as expected:
using UnityEngine;
using System.Collections;
public class HatController : MonoBehaviour {
public Camera cam;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
}
// Update is called once per physics timestep
void FixedUpdate () {
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f);
GetComponent<Rigidbody2D>().MovePosition(targetPosition);
}
}
As you may see - the only thing I have done differently is used GetComponent to get a reference to the object's rigidbody, rather than just stating "RigibBody2D" as a reference itself. I hope this helps someone!
Calling GetComponent in FixedUpdate() is pretty bad advice. Call it in Start(), store it in a local variable, and use that in FixedUpdate ins$$anonymous$$d.
private Rigidbody2D rb2D;
void Start(){
...
rb2D = GetComponent<Rigidbody2D>();
...
}
void FixedUpdate() {
...
rb2D.$$anonymous$$ovePosition(targetPosition);
...
}
That's actually what I had done originally but thought it was an unnecessary convolution. Is it just best practice on the basis that you could have reason to call the rigidbody again later? Or something else? Thanks!
Edit: Oh, wait, I see. It's creating extra work in the update cycle.