- Home /
Guiding a sphere
Hello,
I'm facing a problem I cannot seem to solve, so I thought I'd give forums a try and ask for your help =) Well, I'm trying to guide a sphere. Our player is a Sphere (like any other), and it is forced down the platform by a constant force.
Our spheary player is followed by a camera, meaning, camera is like a 3rd person view (moved back and up, (if we generalize) always looking in direction of spheres velocity).
Now, we want to control this sphere with left and right arrow keys, but we get to several problems - sphere rotates (so we cant use rotation), velocity can't be used (because we are moving straight, and turn sides - example vector goes form 3,0,0 to 0,0,3). We can't use torque either. A problem lies in the sphere itself, I guess - if it were a box, I could use a simple rotation to guide it, but a sphere rotates constantly. And another problem lies in guidance - the camera is always behind the player - pressing W means going straight (by POV) (like in any racing game).
So my question is, how can I control a sphere, turning it left and right (based on the direction it's currently facing)?
Answer by jmgek · Jan 03, 2017 at 06:08 PM
your looking for vector.Left and vector.Right 90% sure, from what I got in your question.
Vector3 = transform.TransformDirection(Vector3.right);
rigidbody.AddForce( myRight * 5 );
And do the same for left.
And you are 90% correct, so your answer is marked correct. However, I have come to a problem: sphere barely turns at the begining, but when it hits a wall, it turns normally afterwards. And, if it hits the wall by a specific angle, it just looses all orientation (maybe rotation has something to do with it?). Any solutions for that perhaps? Thank you.
Answer by jax297 · Jan 05, 2017 at 06:54 AM
So, I couldn't make it work with .addforce - the rotation of sphere after hitting a wall were messing up with .right vector, so turning was basically impossible. What I have done, was set up 2 objects, children of camera object. They were positioned left and right of the sphere. Example: When I turn left, I basically add explosion force to sphere, originating at right side
//START function, get rb of objects positioned left and right of sphere and sphere itself
Rigidbody leftie = GameObject.Find("leftie").GetComponent<Rigidbody>();
Rigidbody rightie = GameObject.Find("rightie").GetComponent<Rigidbody>();
//get sphere rigidbody
Rigidbody rb = GetComponent<Rigidbody>();
//UPDATE function, we add explosion force to our sphere, originating at leftie and rightie
if (Input.GetKeyDown("d"))
{
rb.AddExplosionForce(50f , leftie.transform.position, 1f);
}
if (Input.GetKeyDown("a"))
{
rb.AddExplosionForce(50f, rightie.transform.position, 1f);
}