Thitd Person Rolling Ball?,
HOW TO GET THIS MOVEMENT AND CAMERA?
So basically I want to do a ball movement and camera like the in the new plants vs. zombies game. EXAMPLE VIDEO: link text
The problem is that I want the ball to roll when moving which automatically changes its axis so my horizontal and vertical input dont move the ball into the directions they should. Also I want to rotate the camera around the ball like in a typical 3rd Person Game. I cant use the ball as a parent to the camera otherwise the camera would rotate like crazy...
This is my movement:
public class BallMove : MonoBehaviour
{
public float speed;
public float MouseSensitivity;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetAxis("Mouse X") > 0)
{
//Code for action on mouse moving right
transform.Rotate(0, MouseSensitivity * Time.deltaTime, 0);
}
if (Input.GetAxis("Mouse X") < 0)
{
//Code for action on mouse moving left
transform.Rotate(0, -MouseSensitivity * Time.deltaTime, 0);
}
//Press Space to Jump
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(new Vector3(0, 8, 0), ForceMode.Impulse);
}
//Press Shift to increase Speed
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed * 2;
} else if (Input.GetKeyUp(KeyCode.LeftShift)) {
speed = speed / 2;
}
}
// Update is called once per frame
void FixedUpdate()
{
float vertical = Input.GetAxis("Vertical") * speed;
float horizontal = Input.GetAxis("Horizontal") * speed;
//Movement when pressing WASD
rb.AddForce(transform.forward * vertical);
rb.AddForce(transform.right * horizontal);
//Setting a Speedlimit so it doesnt accelerate infinetly
if (rb.velocity.magnitude > speed)
{
rb.velocity = rb.velocity.normalized * speed;
}
}
}
I tried giving both the ball and the camera the same parent and adding the movement to the parent. So the camera rotates around a parent that doesnt roll like a ball and the ball uses the parent´s axis...BUT to add force the parent needs a rigidbody and the ball too otherwise it would not roll. I tried this but the ball behaves very weird and instantly gets pushed to the side disabling all movement. I found out in another blog that a parent and a child with each having a rigidbody cant work out well. I tried a million other things but it didnt work but as you can the in the video it is possible! Thank you for your help!
,