- Home /
Orbit Camera Around Player And Add Force
I'm a beginner to unity, but I am liking what I have seen so far. I've started working on a project based off of the Roll-A-Ball tutorial in the Learn section, but I'm stuck! I want the camera to rotate around the ball so players can get a better view of the area, but at the same time, have the ball move in the same direction as the camera. Right now I have a simple script the rotates the camera around, but when I press the up arrow key to move forward the ball looks like it's rolling sideways. I have been trying to make a C# script that uses addForce to move it in the right direction, but have failed miserably.
I'm sorry if i'm not being specific enough, and would really be very grateful for any help
CameraRotate↓
using UnityEngine;
using System.Collections;
public class CameraRotate : MonoBehaviour{
public Transform target;
public float distance = 10.0f;
public float sensitivity = 3.0f;
private Vector3 offset;
void Start () {
offset = (transform.position - target.position).normalized * distance;
transform.position = target.position + offset;
}
void Update () {
Quaternion q = Quaternion.AngleAxis(Input.GetAxis ("Mouse X") * sensitivity, Vector3.up);
offset = q * offset;
transform.rotation = q * transform.rotation;
transform.position = target.position + offset;
}
}
PlayerController↓
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 Eno-Khaon · Nov 14, 2015 at 06:05 AM
At the moment, your "movement" Vector3 is strictly defined in world coordinates. If you want your control scheme to change with your camera's perspective, you're going to need to take the camera itself into account.
Therefore, in your PlayerController script, you'll want to make a few adaptations:
public float speed;
// ADDED -- This will keep track of the direction your camera is looking
public Transform cam;
private Rigidbody rb;
// ...
// CHANGED -- This limits movement speed so you won't move faster when holding a diagonal. It's just a pet peeve of mine
Vector2 inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if(inputDirection.sqrMagnitude > 1)
{
inputDirection = inputDirection.normalized;
}
// CHANGED -- This takes the camera's facing into account and flattens the controls to a 2-D plane
Vector3 newRight = Vector3.Cross(Vector3.up, cam.forward);
Vector3 newForward = Vector3.Cross(newRight, Vector3.up);
Vector3 movement = (newRight * inputDirection.x) + (newForward * inputDirection.y);
rb.AddForce(movement * speed);
Okay, now let's break down the changes I made:
First, I added the camera's transform to the script. It should be a simple drag-and-drop.
Second, I put a limit on the input. As I described in the commented script, it's just my personal preference there.
Most importantly, third, I rebuilt a few axes. I created a guaranteed right-facing vector by calculating the vector perpendicular to the camera's forward vector and the world's up vector. While this would almost definitely result in the same vector as "cam.right" would, I use it to account for the possibility that the camera itself leans. This flattens the angle to a (near-) guaranteed "right" from the camera, relative to the world.
Then, I do the same for the forward vector. The "right" vector is now defined, so I make use of that to calculate a new forward, flat against the ground. The camera's pitch is no longer relevant and will not be a factor in moving the ball.
Finally, I implement the new right and forward directions by multiplying them by the two-dimensional inputs. the horizontal axis input (x) is multiplied by the new "right" and the vertical axis input (y) is multiplied by the new "forward" to apply that input to those specific axes.
This worked great. I was just wondering how I could make it so you could change the Y of the camera as well.
Answer by Bassel0 · Nov 09, 2016 at 08:30 AM
I can't drag the camera to the player even though I defined transform called camera that's why the player cannot build its new movement axes depending on the camera
Answer by SoshJam · Aug 10, 2017 at 03:14 PM
Is there a way to stop the camera from going through walls with this script, and/or make it so you can control the camera's Y-axis? @Eno-Khaon
$$anonymous$$oving the camera up and down can be tricky to fine-tune, but should be reasonably simple to implement. Where the camera is set to rotate on the X-axis using...
Quaternion.AngleAxis(mouse$$anonymous$$ovementX, Vector3.up)
... the camera can be tilted up and down using something like...
Quaternion.AngleAxis(mouse$$anonymous$$ovementY, Vector3.right)
... and then concatenated with the previous rotation through an additional multiplication.
$$anonymous$$eeping the camera from passing through walls, however, adds a fair bit of complexity and goes outside the scope of the original question.
I would recommend checking for questions specifically on that topic (and ask about it yourself if you can't find what you're looking for).
Should the inside of the update method look like this then?
Quaternion q = Quaternion.AngleAxis(Input.GetAxis("$$anonymous$$ouse X") * sensitivity, Vector3.up);
Quaternion r = Quaternion.AngleAxis(Input.GetAxis("$$anonymous$$ouse Y") * sensitivity, Vector3.right);
offset = q * offset;
transform.rotation = q * r * transform.rotation;
transform.position = target.position + offset;
There are a lot of different ways to handle camera control, so there's not exactly any specific way for it to work.
For example, it would really just be a matter of opinion whether the Y-rotation should also be factored into the camera's offset position.
// something like...
offset = q * r * offset;
As far as what would work best, however... well, there's not really a best to speak of, so there's no perfect solution to offer anyway.
Your answer
Follow this Question
Related Questions
Roll a ball camera problem? 1 Answer
Flip over an object (smooth transition) 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
How to make the player rotate to where an object is being thrown? 1 Answer
GetKeyUp not registering 2 Answers