Question by
Avasam · Jun 08, 2017 at 06:17 AM ·
movementrigidbody2dmovement scripttranslate
Rigidbody2D.MovePosition() doesn't do anything (but animations and transform.Translate() works)
I'm trying to move the character using Rigidbody2D but despite giving it new coordinates and having no collisions, it simply won't move. The character still animates properly and translating the transforms moves it as well.
Here's my character movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
public Rigidbody2D rbody;
public Animator anim;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per physics timestep
void FixedUpdate()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
anim.SetBool("isWalking", true);
anim.SetFloat("input_x", movement_vector.x);
anim.SetFloat("input_y", movement_vector.y);
}
else
{
anim.SetBool("isWalking", false);
}
Debug.Log("Rigidbody2D current pos: "+ rbody.position);
Debug.Log("Movement vector2: "+ movement_vector);
Vector2 newPosition = rbody.position + movement_vector;
Debug.Log("New position: " + newPosition);
rbody.MovePosition(newPosition);
//transform.Translate(movement_vector.x/16, movement_vector.y/16, 0);
}
}
And here's the console output:
Rigidbody2D current pos: (31.5, -33.7)
UnityEngine.Debug:Log(Object)
CharacterMovement:FixedUpdate() (at Assets/Scripts/CharacterMovement.cs:34)
Movement vector2: (0.0, 0.0)
UnityEngine.Debug:Log(Object)
CharacterMovement:FixedUpdate() (at Assets/Scripts/CharacterMovement.cs:35)
New position: (31.5, -33.7)
UnityEngine.Debug:Log(Object)
CharacterMovement:FixedUpdate() (at Assets/Scripts/CharacterMovement.cs:38)
Comment
$$anonymous$$ovement vector2: (0.0, 0.0)
If your movement vector is null, how do you expect this should move your rigid body at all?
Your answer
Follow this Question
Related Questions
Y-axis movement stuck. Help me please :c,Not Moving by pressing Button :C. Help me please 0 Answers
2D Using C# moving game object back and forth and random stop in between - all in x-axis 2 Answers
Rigid body vibrating? 0 Answers
transform.Translate is too smooth 3 Answers
Face direction of a Vector 3 1 Answer