x


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.

more ▼

asked Apr 05 '10 at 05:44 PM

Ethan gravatar image

Ethan
21 1 1 3

or like pressing alt left click in the scene window while using the scroll wheel. I would like that too.

Apr 05 '10 at 06:04 PM alexnode
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
    }
}
more ▼

answered Apr 05 '10 at 06:17 PM

qJake gravatar image

qJake
11.6k 43 78 161

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

Apr 05 '10 at 08:00 PM Ethan

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?

Apr 05 '10 at 08:25 PM Ethan

Ah Nevermind! This is a C# Script. I think I can get those values correct shouldn't be too hard.

Apr 05 '10 at 11:36 PM Ethan

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?

Apr 05 '10 at 11:57 PM Ethan

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.

Apr 06 '10 at 08:18 AM alexnode
(comments are locked)
10|3000 characters needed characters left

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.

Input Settings

MCE Settings

more ▼

answered Apr 06 '10 at 09:41 PM

qJake gravatar image

qJake
11.6k 43 78 161

I'll get these all matched up and tell you how that goes. Again thanks for all this.

Apr 07 '10 at 01:32 AM Ethan

I keep getting a console error that it needs the Move Camera Enabler Inputs.

Apr 07 '10 at 07:56 PM Ethan

Edited my post to add it in. Sorry about that :)

Apr 07 '10 at 10:41 PM qJake

It appears to work, sort of, if I mess with the camera after a few seconds Unity locks up and crashes.

Apr 07 '10 at 11:52 PM Ethan

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.

Apr 08 '10 at 12:24 AM qJake
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 28 '11 at 04:38 AM

user-9770 (google) gravatar image

user-9770 (google)
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2996
x181
x155
x5

asked: Apr 05 '10 at 05:44 PM

Seen: 3611 times

Last Updated: Apr 05 '10 at 05:44 PM