- Home /
GameObject won't move with transform.translate or rigidbody2d.moveposition,GameObject won't move in void Start()
So I'm trying to move a ball. The ball has a rigidbody2d and circle collider 2d for that sweet bounce material, but the ball won't move at all. Here's my code:
void Start()
{
_ballMoveSpeed = 2f;
_ballRigidBody = _ball.GetComponent<Rigidbody2D>();
_direction = new Vector3(Random.Range(-1, 2), Random.Range(-1, 2), 0);
1:)_ball.transform.Translate(_direction * _ballMoveSpeed * Time.deltaTime, Space.World);
2:)_ballRigidBody.MovePosition((_ball.transform.position + _direction) * _ballMoveSpeed * Time.deltaTime);
}
I've tried transform.translate (example 1) but nothing.
Tried using rigidbody2d.moveposition (example 2) still nothing. Tried increasing the speed also no movement. I get the _direction through debug logs and when I put a log at the end it gets there but the ball still won't move. What am I doing wrong? Thanks!
Edit: I tried using rigidbody2d body types dynamic and kinematic and added and removed mass still nothing. But when I changed gravity scale from 0 to 1 it started to fall as expected. That's the only movement I got from it so far.
Edit 2: I also converted the Vector3 _direction to Vector2 and still nothing
Answer by racpan · Jan 20, 2021 at 01:55 AM
I used transform.translate method but I put it in FixedUpdate().
I'm not sure if that's enough but I also made the ball gameobject it's own parent, I used to have it as a child under a gameobject that has a 1,1,1 scale.
I just tested it again. Having the ball as a child under a 1,1,1 scaled game object doesn't let it move so I moved it outside and now it works fine.