- Home /
Player2d Movement (rigidbody2d.velocity) with small vibrations, very similar to small collisions
Hi guys, I'm trying to move my player in horizontal axis, i was using transform.position to move my player but looking some articles i saw that is better using rigidbody.velocity to move. I changed and it worked but now i can see my player trembling (small vibrations, very similar to small collisions).
When i use transform.position for my movement, work perfectly.
I will put my code here and if anybody can help me, i appreciate.
==========================
Complete code: https://github.com/MAAARKIN/InitAndroidGame/blob/master/Assets/Scripts/Players/Player.cs
if (goToLeft && !goToRight) {
this.transform.eulerAngles = new Vector3(0, 180, 0);
this.animator.SetBool("isWalking", true);
//this.transform.position += Vector3.left * speed * Time.deltaTime; //WORK PERFECTLY
this.rigid2D.velocity = new Vector2(-speed, rigid2D.velocity.y); //small vibrations.
} else if (goToRight && !goToLeft) {
this.transform.eulerAngles = new Vector3(0, 0, 0);
this.animator.SetBool("isWalking", true);
//this.transform.position += Vector3.right * speed * Time.deltaTime; //WORK PERFECTLY
this.rigid2D.velocity = new Vector2(speed, rigid2D.velocity.y); //small vibrations.
}
Answer by Dream_in_code · Aug 29, 2016 at 03:29 PM
public float maxSpeed;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
void Start()
{
myRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
facingRight = true;
}
//for movement
float move = Input.GetAxis("Horizontal");
myAnim.SetFloat("speed", Mathf.Abs(move)); //playing animation//
myRB.velocity = new Vector2(move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight)
{
flip();
}
else if (move < 0 && facingRight)
{
flip();
}
}
void flip()
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
This is just another way to move. this not answer my question, my problemm is with vibrations from rigidbody and not to move my player.
Thx to answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help Rotating a Player 0 Answers
how do I make a obeject (player) move the way it's pointing ? (like a jetpack) (c#) 1 Answer
Controls stop working on computer when hooked up to Unity Remote 0 Answers
Type or name space cannot be found 1 Answer
Horizontal and Vertical Input already receiving input? 0 Answers