- Home /
Directional rolling a ball ( upwards / left only )
Hi all.
I am moving a ball gameobject via this simple code. Aka each time the user taps the direction changes upwards or to the left. How can I addForce to the ball so the ball rolls nicely towards that direction. So what is going on is: Each time the player clicks, the ball moves towards an up or left direction + the amount to move which is defaulted as 10.
I provided the code which handles all of this. But somehow my ball doesn't roll. I might overlook a simple fact, and i am pretty sure it has to do with the translation. But it's very important the ball moves by 10 increments, and i am struggling to make it roll but keep in mind it can only roll towards the increment.
Hope my question is clear. I've searched quite a bit but can't get it to work.
This script is attached to the player directly
Thank you for your wisdom ;)
void Update ()
{
if (Input.GetMouseButtonDown(0) && !isDead)
{
if (dir == Vector3.forward)
{
dir = Vector3.left;
}
else
{
dir = Vector3.forward;
}
}
float amoutToMove = speed * Time.deltaTime;
//needs force to roll the ball
rigidbody.AddForce(dir);
transform.Translate(dir * amoutToMove);
}
Somehow I can't get it to work.
Does anyone have an idea please? :) I am patient however, it buggers me that I can't find such a simple sollutions
Thank you.
you are mixing kinematic and not kinematic code. you should not use both. decide if you want physics to handle the movement or do you want to handle movement yourself.
if you want to use transform.Translate() to move the ball yourself, you should use transform.Rotate() to roll the ball yourself and delete rigidbody.AddForce()
Thanks, yes I tried that at first to no avail. In the meantime if found a solution. Add a movement script on a parent object. And a rotation script on the child which references to the parent movement script to get the current direction. F$$anonymous$$ If the movement script dir = V3.left, then my if statement in the roll class rolls towards the correct axis.It's still not perfect but so far i can test my game idea.
Otherwise when you roll in the direction you move, the ball starts jumping up and down and all that. This isn't true when no gravity is applied. But in my book, gravity is a nice game mechanic that adds ( literally )
Thanks for your answer, I really appreciate any input. All the best.