- Home /
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));
}
}
Answer by Jessy · Jan 21, 2011 at 09:01 PM
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.
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.
Answer by Slaptones · Sep 24, 2011 at 03:50 PM
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));
}
Answer by Slaptones · Sep 24, 2011 at 03:50 PM
Hello i have a solution for you
var cameraSpeed : float = 1.0; var bounceRight : float = 1.0; var bounceLeft : float = 1.0; function Update() { var viewPos = Camera.main.WorldToViewportPoint (transform.position);var speed : float = 6.0;
//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)); }
Your answer
Follow this Question
Related Questions
2D boundaries for player gameobject not moving with scrolling camera 1 Answer
Deadfrontier type crosshair 1 Answer
WebGL: Scroll Camera 0 Answers
Need help with 2D orthographic camera zooming 2 Answers
raycast not doing good 1 Answer