- Home /
Orbit around player, but allow re-centering of camera via button press?
First off, to be clear I'm using JavaScript.
So I created a script that allows me to reset the Y-axis of the main camera when pressing a button. I spent all day today trying to figure out why it didn't work, and I believe now after messing with a simple project (and enabling/disabling the Mouselook or Mouse Orbit scripts) that it's the orbit script that prevents me from resetting the camera's position.
I don't quite understand how the mouse orbit works, so perhaps I'm just going about this the wrong way.
Anyway, here's my reset camera script(ResetCam.js):
#pragma strict
var Cam : GameObject;
var originalYPos : float;
function Start () {
originalYPos = 25.0;
}
function Update () {
if (Input.GetButtonDown("f")) {
print(transform.eulerAngles.y);
Cam.transform.eulerAngles.y = originalYPos;
print("after:" + " " + transform.eulerAngles.y);
}
}
With the Mouselook script enabled on my camera, I can rotate freely (but it doesn't orbit the player, which is what I need.) With Mouse Orbit script on, I can rotate around the player, but when trying to press the above input button to reset the y-axis rotation back to it's initial value, I get nothing.
My guess is that the orbit script has priority over the camera's rotation and nothing I do will allow me to actually see the camera reset when pressing my input button. I put a "print" line in my script so I could see if it was actually getting changed, and it is, but you can't see the change with Mouse Orbit on, only with Mouse Look.
So my question is, is there something I'm missing? Is there a way to allow the camera to still orbit the player, but when pressing a keyboard key the camera's rotation on the Y-axis gets reset?
Also, I apologize if this has been discussed prior, but I did research on resetting the camera and couldn't find anything that helped.
I'm really trying to add this as a 'center camera' feature for a game I'm working on, much like other 3d games where you can center the camera via button push.
I've created a video demonstration that shows an example of what I'm going for, although it's on the vertical axis rather than the horizontal axis:
Also, here's the MouseOrbit script:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Answer by Matthew012 · Aug 29, 2013 at 03:42 AM
function Update () {
if (Input.GetButtonDown("g")) {
GameObject.FindObjectByTag("NAME").GetComponent(OtherScript).enableRotate = true; // starts rotation
}
if (enableRotate) { //Checks to see if it is true and while it is true it does what is in the statement.
Cam.transform.eulerAngles.y ++;
}
if (Input.GetButtonDown("f")) {
print(transform.eulerAngles.y);
GameObject.FindObjectByTag("NAME").GetComponent(OtherScript).enableRotate = false; // starts rotation // Stops rotation
Cam.transform.eulerAngles.y = originalYPos;
print("after:" + " " + transform.eulerAngles.y);
}
}
var enableRotate: boolean; // Add this in this script and make sure it is public var.
function LateUpdate () {
if (enableRotate) {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
}
There I edited my answer and try this. Make sure you put the corrections and change out my spots as NAME and OtherScript and replace it with the proper ones. It should stop it.
I just read up on the question some more (Stupid sleepy me), but copy and paste this in your resetCam.js and run it but disable the other movement script and see if it works properly if so, just try to duplicate what I did.
Thanks for the response, but your solution isn't quite what I'm looking for and it only worked when I turned $$anonymous$$ouse Orbit off. (I still need to be able to stay with the player and orbit around it.) When hitting "g", the camera rotates to the right until I hit "f."
I'm not looking to rotate the camera (sorry if my initial post was worded improperly or mislead you) continuously on a button press, but reset the Y back to it's initial value on game start if the player had rotated the camera away while playing.
$$anonymous$$ain Camera needs to center back behind the player character upon pressing the button. I wish I could show you what I mean via video, but I can't think of any video examples off the top of my head, nor how to find any.
Well if you're free around let say 1, 2, or after 6pm Eastern Time Zone we can use $$anonymous$$m viewer and show me directly (if no one posts the answer you want) cause it might help me to see it. Also if you could upload the rotations script as well it might help cause it can all be dependent on how you have it setup cause right now what I see is you have nothing stopping the rotation. Which is what my enable var was for. It stopped the rotation. I hate typing on the iPhone... Stupid auto correct...
Okay, so I made a little crappy image to demonstrate what I'm trying to say:
The orange line represents the Y axis for when the camera is being rotated by the mouse. The first red circle is where the camera begins. Next, I move my mouse positive Y and so the second red circle is where the object appears to be on screen.
The white arrow pointing back to the first red circle signifies me pressing "f," which should return the camera's rotation back to where it was before, except I can only get this to happen if I use just mouselook and I don't orbit the player.
Answer by brett nieland · Aug 30, 2013 at 09:02 PM
Here is a reduced version my c# script on my camera:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
private class CSimpleTransform
{
public Vector3 position {get;set;}
public Quaternion rotation {get;set;}
}
public Camera PerspectiveCamera;
CSimpleTransform perspectiveCameraOriginalTransform;
public float force = 1000.0f;
void Start ()
{
PerspectiveCamera = GameObject.Find("Perspective Camera").camera;
perspectiveCameraOriginalTransform = new CSimpleTransform();
perspectiveCameraOriginalTransform.rotation = PerspectiveCamera.transform.localRotation;
perspectiveCameraOriginalTransform.position = PerspectiveCamera.transform.localPosition;
}
// Update is called once per frame
void FixedUpdate ()
{
HandleKeyboard();
}
void HandleKeyboard()
{
if (Input.GetKeyDown(KeyCode.Space))
{
PerspectiveCamera.transform.localRotation = perspectiveCameraOriginalTransform.rotation;
PerspectiveCamera.transform.localPosition = perspectiveCameraOriginalTransform.position;
}
}
}
Is there any chance you can convert this to a JavaScript(.js) file? I'm not familiar with C# (sorry) and when trying to add this script as a .cs file to my camera, I get a "can't add script" pop-up error talking about how the class in the script doesn't match the filename. I named the file ResetCamCSharp.cs
I did try turning it into a javascript file, but not sure how to deal with class for example. Some things just don't work when trying to convert into a .js file.
@Lorcan, I do not know Unity js syntax well enough to do this. I suggest you leave it in c# and try replacing the line. public class GameController : $$anonymous$$onoBehaviour with
public class ResetCamCSharp : $$anonymous$$onoBehaviour
Also, you will need to replace "Perspective Camera" with the name of your camera.
Right, I noticed that part of it. Thanks though, I'll post again soon.
Okay, I fixed the lines and pressed space, but nothing happened.
Don't know what to tell you. Try doing some debugging. http://www.youtube.com/watch?v=vhT$$anonymous$$5weADhU
Your answer
Follow this Question
Related Questions
help with my orbit camera script 0 Answers
Rotate an object around another on input 0 Answers
Reposition camera behind the player character during movement 1 Answer
3drd person camera reset 0 Answers
Rotate camera to object on sphere 1 Answer