x


camera boundary and movement in rtype style scrolling shooter

Hi,

I am trying to make a plain side scrolling spaceship shooter R-Type style (if someone doesn't know about this legend here is a youtube link: link text)

What this means is that : 1. camera is slowly moving from left to right (x axis in my case) 2. player cannot control camera movement 3. spaceship is constantly passively moving with the camera (he won't be "left behind") 4. spaceship has a freedom to actively move inside the camera viewport boundaries

So far i have made a simple script that tries to implement this behaviour but i am not very happy with it. Not happy at all.

The problem i am having is defining camera viewport boundaries. While i have been more or less successful in preventing it moving up or down off the screen (+-y axis in my case) i can still see some microbounciness of my ship, especially when trying to prevent it to move right off the screen (+x axis) where i needed to introduce a bounceRight variable to further compensate the bounciness (i am not smart enough to calculate the compensation so i used the trail and error method). Where i almost completely failed is to prevent it to move left of the screen (-x axis). Unless i define a really big bounceLeft variable, which launches my ship into bouncy frenzy...it simply floats of the screen.

Is there a better way to do this? Or can i somehow improve my script?

var speed : float = 6.0;
var cameraSpeed : float = 1.0;
var bounceRight : float = 1.0;
var bounceLeft : float = 1.0;


function Update() {
    //player movement
    var controller : CharacterController = GetComponent(CharacterController);
        moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0);
        moveDirection *= speed;
        moveDirection.x += cameraSpeed;
        controller.Move(moveDirection * Time.deltaTime);

    //camera movement
    Camera.main.transform.Translate(Vector3(cameraSpeed*Time.deltaTime,0,0));

    //viewport boundaries
        var viewPos = Camera.main.WorldToViewportPoint (transform.position);

        if( viewPos.x < 0 ) {
            controller.Move(Vector3(-moveDirection.x * bounceLeft * Time.deltaTime,0,0));
        }
        else if( viewPos.x > 1 ) {
            controller.Move(Vector3(-moveDirection.x * bounceRight * Time.deltaTime,0,0));

        }
        if( viewPos.y < 0 ) {
            controller.Move(Vector3(0,-moveDirection.y * Time.deltaTime,0));

        }
        else if( viewPos.y > 1 ) {
            controller.Move(Vector3(0,-moveDirection.y * Time.deltaTime,0));
        } 


}
more ▼

asked Jan 21 '11 at 08:55 PM

mpupovac gravatar image

mpupovac
3 3 3 6

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

A Character Controller is not a good match for this type of game. Why are you using it?

When you take CharacterController.Move out of the equation, you can simply clamp transform.position values.

more ▼

answered Jan 21 '11 at 09:01 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

Eventually i ended up with attaching rigidbody to a spaceship and using a velocitychange forces to move it. Then i made boundaries with 4 static collider cubes around the scene which are created on runtime and set as camera children.

Jan 23 '11 at 10:39 PM mpupovac
(comments are locked)
10|3000 characters needed characters left

Hello!

sorry about my English is not very good, I will use google translator. I am learning Unity in my spare time, after setting aside a project I'm doing with some friends, I decided to do on my own practice of a game like R-type.

I was not sure where to start, so I found this post and decided to use his script, I ask permission for it.

I found an inelegant solution to their problem of the limits of the map, this rebound is the ship.

I have a solution to the problem and wanted to share. Not explain very well in English, I think I understand better by seeing the source code. Thank you very much for posting this idea has served me well. var speed : float = 6.0; var cameraSpeed : float = 1.0; var bounceRight : float = 1.0; var bounceLeft : float = 1.0;

function Update() {

var viewPos = Camera.main.WorldToViewportPoint (transform.position); //player movement var controller : CharacterController = GetComponent(CharacterController);

var h = Input.GetAxis("Horizontal");

var v = Input.GetAxis("Vertical");

if (h <0 && viewPos.x <=0.1) { h =0; }

if (v >0 && viewPos.y >=1) { v=0; } moveDirection = Vector3(h,v,0); moveDirection *= speed; moveDirection.x += cameraSpeed; controller.Move(moveDirection * Time.deltaTime);

//camera movement Camera.main.transform.Translate(Vector3(cameraSpeed*Time.deltaTime,0,0));

}

more ▼

answered Sep 24 '11 at 03:50 PM

Slaptones gravatar image

Slaptones
1

(comments are locked)
10|3000 characters needed characters left

Hello i have a solution for you

var speed : float = 6.0; var cameraSpeed : float = 1.0; var bounceRight : float = 1.0; var bounceLeft : float = 1.0;

function Update() { var viewPos = Camera.main.WorldToViewportPoint (transform.position);

//player movement

var controller : CharacterController = GetComponent(CharacterController);

var h = Input.GetAxis("Horizontal");
var v = Input.GetAxis("Vertical");

if (h <0 && viewPos.x <=0.1) {
    h =0;
}

if (v >0 && viewPos.y >=1) {
    v=0;
}   
    moveDirection = Vector3(h,v,0);
    moveDirection *= speed;
    moveDirection.x += cameraSpeed;
    controller.Move(moveDirection * Time.deltaTime);

//camera movement
Camera.main.transform.Translate(Vector3(cameraSpeed*Time.deltaTime,0,0));

}

more ▼

answered Sep 24 '11 at 03:50 PM

Slaptones gravatar image

Slaptones
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:

x2989
x129
x56
x27
x1

asked: Jan 21 '11 at 08:55 PM

Seen: 1844 times

Last Updated: Sep 24 '11 at 03:50 PM