Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mpupovac · Jan 21, 2011 at 08:55 PM · camerashooterscrollingboundary

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));
     } 


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mpupovac · Jan 23, 2011 at 10:39 PM 1
Share

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.

avatar image
0

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));

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Slaptones · Sep 24, 2011 at 03:50 PM

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)); }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges