Why is my camera rolling around like a ball?
I'm new to unity, I'm trying to figure out how to do the rolling ball tutorial game to start off, and the next lesson I'm learning is how to make the camera follow the ball. I put the camera as a "child" of the player object (the ball) and when I hit play to see if the camera followed my ball, I found out that it did... way too well. The camera rolls like the ball does so the entire camera screen is rotating and spinning and frankly makes me feel a bit sick watching it. Is this because in my script the ball rolls, so making the camera a child of the ball makes the camera roll too? I can't figure out how to fix this nonsense and I need to to continue learning unity :/
here is the tutorial i'm using: https://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-camera?playlist=17141
Here is my script attached to my player: using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public float speed; private Rigidbody rb;
void Start (){
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
Answer by Taorcb · Aug 26, 2015 at 08:45 AM
To elaborate on the "it rolls because it's a child of the ball" answer: If you set a GameObect to be a child of another GameObject, the child's position will always be the same, relative to it's parent's position. I would use a transform.LookAt() function to keep the camera stable, but the tutorial you're doing will cover that.
Answer by AtticRoomBoy · Aug 25, 2015 at 03:14 PM
The Roll a Ball tutorial does show you how to write a script to fix this.
The reasons the camera rolls around like this is because it is a child of the ball.
Answer by SuperRaed · Aug 25, 2015 at 07:35 PM
Like AtticRoomBoy said having it as a child is causing it to roll along with the ball and I know what's that like as I fell for the same mistake. why don't you try to set the transform.position for your camera as the ball.transform.position? but keep in mind to keep a distance vector 3 variable so that with each frame update the camera position would be
void FixedUpdate(){
transform.position = balls.transform.position+ difference.position
}
Your answer
Follow this Question
Related Questions
Third person controller camera 1 Answer
Camera Roll/Headbob? 0 Answers
How to fix player camera shaking 0 Answers