- Home /
Camera Rotation, Side to top-down view
Hi all, I'm creating a game and I need to use two different view points, I can do this using two cameras and a toggle switch on key press BUT this is like switching a light on and off.... I want to do this with a single camera that starts at a set point but follows the player and then with a button press the camera rotates 90 on the X and still looking at the player. the script needs to then work in reverse. I'm new to unity and this is for a uni module however we're not getting any input on how to use unity.
please help :)
Post the code you have written, and explain why it doesn't do what you want. You know how to do rotation, right? That's in the docs.
Answer by sniper43 · Feb 24, 2015 at 12:39 PM
Make the camera a child of the observed object, then simply adjust it's variables: mostlikey just transform.position and transform.rotation, or more simply just the transform.
Example code (that you will most likely copy since it should do EXACTLY what you need it to do):
private Transform firstVP;
private Transform secondVP;
void Start()
{
firstVP.localPosition = new Vector3(0, 2, 0);
secondVP.localRotation = Quaternion.Euler(0,-30, 0);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.V))
transform = (transform == firstVP ) ? secondVP : firstVP; //not sure if transform supports "==" operator, but here is the reference for the "?" operator in case it confuses you https://msdn.microsoft.com/en-us/library/ty67wk28.aspx
}
In case you want more viewpoints just make a Transform array (so Transform[]) and use a counter to cycle through.
The above code would look like:
private Transform[] Viewports; private int currentViewport;
void Start()
{
currentViewports = 0;
Viewports = new Transform[2];
//TODO, add positions/rotations here
}
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.V))
transform = Viewports[currentViewports++]; //no further code needed, as ++ increase the value AFTER it has been used, alternatively use ++currentViewports to increase BEFORE it has been used> same llogic applies to --
}
Thank you guys, I will have a look and a play with these and I will post an update, I'm making a game at the moment for UNI and we're not getting any input other than follow the tutorials on unity and then make your own AA game........ a ga$$anonymous$$g degree in Cambridge which costs 9k a year, which can be done at home, online......
http://answers.unity3d.com/page/faq.html
Read through that. If you're gonna be posting new questions, you're going ot need it.
The above let's you cycle through preset positions, as was the original question. If you want another way to do there are way to do it, but that is irrelevant to the question, so the question should be closed.
Answer by etneeko · Feb 26, 2015 at 11:01 PM
Hi sniper43, I've had a look at the first code you added on here as its in c# but I cant get it to work there are a few errors which I dont understand.
is there not an easier way to rotate the camera with a button press ?
Post these things as comments, not answers. It sitll sends me an email notification, so adding a comment > posting an answer, since answers are supposed ot be answers, not comments.
Delete this answer, and comment in what the error code is. If it is a new error because of another issue PLEASE post it as a seperate question, or search UA for an answer.
And no there isn't. Welcome to the world of software design, where computers are $$anonymous$$dless morons that can only do what you tell them to do.
ah sorry I didnt even see the option until you pointed it out, okay so I've been working hard to get this done and I now have something that works.... It does what I need it to do however I want to tweek it but I'm at a wall now.
Okay so here is my code
using UnityEngine; using System.Collections;
public class CameraController : $$anonymous$$onoBehaviour {
public GameObject targetObject;
private float targetAngle = 0;
const float rotationAmount = 1.5f;
public float rDistance = 1.0f;
public float rSpeed = 1.0f;
// Update is called once per frame
void Update()
{
// Trigger functions if Rotate is requested
if (Input.Get$$anonymous$$eyDown("r")) {
targetAngle -= 90.0f;
} else if (Input.Get$$anonymous$$eyDown("t")) {
targetAngle += 90.0f;
}
if(targetAngle !=0)
{
Rotate();
}
}
protected void Rotate()
{
float step = rSpeed * Time.deltaTime;
float orbitCircumfrance = 2F * rDistance * $$anonymous$$athf.PI;
float distanceDegrees = (rSpeed / orbitCircumfrance) * 90;
float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * $$anonymous$$athf.PI;
if (targetAngle>0)
{
transform.RotateAround(targetObject.transform.position, Vector3.right, -rotationAmount);
targetAngle -= rotationAmount;
}
else if(targetAngle <0)
{
transform.RotateAround(targetObject.transform.position, Vector3.right, rotationAmount);
targetAngle += rotationAmount;
}
}
}
as you can see I have getkeydown on the R and T keys for the two way rotation... so moving from one to the new and back again. I would like this all on the same key so first press does teh new rotation and then on the secend press it moves back again. I'm also finding that once my camera rotates teh player no longer moves...
Any Ideas?
Your answer
Follow this Question
Related Questions
Check if position is on screen 0 Answers
Art is Distorting in 2.5D Project 0 Answers
Having issues with a smooth camera follow 2 Answers
2.5D Shooter Rescrict Movement to Camera View 2 Answers
Smoother camera movement? 2 Answers