- Home /
How to make a Homeworld Style Camera.
I'd like to know how I would get this to work correctly. For those who have played Homeworld 1 or pretty much any space RTS you are able to move the camera around by holding the right mouse button and then moving the mouse. You are also able to scroll in and out using the scroll wheel. I've seen some things like this around here but they have all mainly been for Land based RTS's and haven't given me the desired result even with a bit of tinkering. Any Help is appreciated.
or like pressing alt left click in the scene window while using the scroll wheel. I would like that too.
Answer by qJake · Apr 05, 2010 at 06:17 PM
This script will allow you to assign (using the Input Manager), two axes for Camera Movement, one for "Camera Enable" (which, in your case, should be the right mouse button), as well as camera rotation and a "Camera Rotation Enabler" (which, for my game, was the middle mouse button).
So the end result of this script, after all the input axes are setup correctly, is:
Hold RMB to drag and move the camera around. Hold MMB (Scroll Wheel) and drag left or right to rotate the camera around.
I don't remember what the values were inside of the Input Manager (and I don't think I have the project anymore), so you'll need to figure that out yourself by reading this script and assigning values to the functions noted in the script. This will NOT work by just copy-pasting it!
Input Values that need to be added:
- Camera X - Button for left/right camera (like the keyboard arrows)
- Camera Y - Button for up/down (like the keyboard arrows)
- Move Camera Enabler - Button for enabling camera mouse movement (like the RMB)
- Camera X Mouse - Mouse X Axis
- Camera Y Mouse - Mouse Y Axis
- Camera Rotation - Middle mouse button (typically mouse button 1 or 2).
- Camera Zoom - Mouse Scrollwheel for zooming in and out (Mouse Axis 3).
using UnityEngine; using System;
[AddComponentMenu("Camera Control/Smooth Follow")] class SmoothFollow : MonoBehaviour { public float CameraMoveSpeed = 60; public float CameraMoveMouse = 40; public float CameraZoomSpeed = 30; public float CameraRotateSpeed = 30; public float Camera = 4;
const float CameraMax = 50;
const float CameraMin = 15;
void LateUpdate()
{
if (hasInput())
{
float thiseulerX = transform.rotation.eulerAngles.x;
transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, transform.eulerAngles.z);
if (Input.GetAxis("Camera X") > 0)
{
transform.Translate(Vector3.forward * Time.deltaTime * CameraMoveSpeed);
}
if (Input.GetAxis("Camera X") < 0)
{
transform.Translate(Vector3.back * Time.deltaTime * CameraMoveSpeed);
}
if (Input.GetAxis("Camera Rotation") == 0)
{
if (Input.GetAxis("Camera Y") > 0)
{
transform.Translate(Vector3.right * Time.deltaTime * CameraMoveSpeed);
}
if (Input.GetAxis("Camera Y") < 0)
{
transform.Translate(Vector3.left * Time.deltaTime * CameraMoveSpeed);
}
}
if (Input.GetAxis("Move Camera Enabler") != 0)
{
Screen.lockCursor = true;
if (Input.GetAxis("Camera X Mouse") != 0)
{
transform.Translate(Vector3.forward * Time.deltaTime * CameraMoveMouse * Input.GetAxis("Camera X Mouse"), Space.Self);
}
if (Input.GetAxis("Camera Y Mouse") != 0)
{
transform.Translate(Vector3.right * Time.deltaTime * CameraMoveMouse * Input.GetAxis("Camera Y Mouse"), Space.Self);
}
}
else if (Input.GetAxis("Camera Rotation") != 0)
{
Screen.lockCursor = true;
if (Input.GetAxis("Camera X Mouse") != 0)
{
transform.Rotate(Vector3.up * Time.deltaTime * CameraRotateSpeed * Input.GetAxis("Camera Y Mouse"), Space.World);
}
}
transform.rotation = Quaternion.Euler(thiseulerX, transform.eulerAngles.y, transform.eulerAngles.z);
if (Input.GetAxis("Move Camera Enabler") == 0)
{
if (Input.GetAxis("Camera Rotation") == 0 && Input.GetAxis("Camera Zoom") != 0)
{
transform.Translate(Vector3.forward * Time.deltaTime * CameraZoomSpeed * Input.GetAxis("Camera Zoom"), Space.Self);
while (transform.position.y > CameraMax)
{
transform.Translate(Vector3.forward * Time.deltaTime / 100, Space.Self);
}
while (transform.position.y < CameraMin)
{
transform.Translate(Vector3.back * Time.deltaTime / 100, Space.Self);
}
}
}
}
else
{
Screen.lockCursor = false;
}
}
bool hasInput()
{
if (
Input.GetAxis("Camera X") != 0 ||
Input.GetAxis("Camera Y") != 0 ||
Input.GetAxis("Move Camera Enabler") != 0 ||
Input.GetAxis("Camera Zoom") != 0 ||
Input.GetAxis("Camera Rotation") != 0
)
{
return true;
}
return false;
}
}
WOW Thanks. Sadly I am new to unity so i'm not sure how exactly to get this running but I'll toy with it and see what I can do. Thanks
When I edit the values (which i may be doing wrong) even after I do I play the game to test my camera and I get a ton of "no semi colon" Errors. Any Ideas?
Ah Never$$anonymous$$d! This is a C# Script. I think I can get those values correct shouldn't be too hard.
Heh Heh. I am probably really get annoying and I apologize. As if you will ever see this. I can't seem to get the values correct. This will be the last time I ask but any Ideas?
It is a bit advanced, unfortunately. I think you have to strip it down and understand it in depth, compare it with the default mouse look in the first person controller too, might help.
Answer by qJake · Apr 06, 2010 at 09:41 PM
Okay... here's all the relevant input settings from that project. Everything needs to match pretty much exactly for it to work (the names especially).
Edit: This is far from perfect... I have duplicate input types, and gravity doesn't need to be "9999"... but if you plug these values in it should work.
I'll get these all matched up and tell you how that goes. Again thanks for all this.
I keep getting a console error that it needs the $$anonymous$$ove Camera Enabler Inputs.
It appears to work, sort of, if I mess with the camera after a few seconds Unity locks up and crashes.
I dunno what the problem is... you may have another conflicting script on your camera or something, I really don't know... you'll just have to play with it and try to make it work.
Answer by user-9770 (google) · Feb 28, 2011 at 04:38 AM
Following this example word-for-word, I end up crashing Unity. Not cool.
Seems to be something specifically with the Camera Zoom. It would be nice if someone could update this with something less suboptimal.
Your answer
Follow this Question
Related Questions
Unity Scene Dimensions 1 Answer
move camera to clicked object 0 Answers
Smooth camera height adjustment based on terrain 1 Answer
How to make camera position relative to a specific target. 1 Answer
Camera Orbit for spaceship 1 Answer