- Home /
Character dragging ground with it.
I have a character and a moving platform. The platform functions fine when the player is not moving. The platform defaults to moving left at a constant velocity. However when I move the character, it drags the platform along with it, so instead of the player moving, the platform and player are moving together with no player movement with respect to the platform. How would I get the player to not affect the platform velocity when moving? I have rigidbodies on both the player and the platform.
Are you using "RigdBody.addForce" or "transform.Translate". It is 2d sprites or you are using 3d object with ortographic perspective to get the 2d illusion. Post your code for both,player and moving plataform.
I am using addForce on 2d sprites. So, I've changed my code a bit since then by trying to make only the ground moving and locking the x location of the character. But currently this causes the ground to stay in place ins$$anonymous$$d of moving also. Here are the relevant code:
public class Road : $$anonymous$$onoBehaviour {
public float speed = -0.5f;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
if (rb2d.velocity.x > speed)
rb2d.AddForce(Vector2.left);
else if (rb2d.velocity.x < speed)
rb2d.AddForce(Vector2.right);
float h = Input.GetAxis("Horizontal");
rb2d.AddForce(Vector2.right * h * speed);
}
}
public class Player$$anonymous$$anager : $$anonymous$$onoBehaviour {
public float gravity = 1f;
public float jumpPower = 4f;
public bool grounded;
private Rigidbody2D rb2d;
private float speed = 0;
//private Animator anim;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
rb2d.constraints = RigidbodyConstraints2D.FreezePositionX
| RigidbodyConstraints2D.FreezeRotation;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Here is the original call to move the player
//rb2d.AddForce(Vector2.right * 3 * (baseSpeed - rb2d.velocity.y));
}
void FixedUpdate()
{
if(Input.GetButtonDown("Jump"))
{
speed = jumpPower;
}
}
}
Answer by Grench1 · Jan 02, 2017 at 08:25 AM
I really recomend you to use Transform.Translate instead of Rigidbody.addForce. For the player:
void FixedUpdate()
{
transform.Translate (new Vector3(Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed,0,0));
}
and for the ground:
void FixedUpdate()
{
transform.Translate (Vector3.left * speed * Time.fixedDeltaTime);
}