• 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
Question by DayyanSisson · Aug 15, 2011 at 05:09 PM · speedflightconstantforcein-gameflight sim

Increase Speed in Gameplay

I'm making a Space Sim. I need to have my ship in constant motion forward. I have that with my script, so I need to make it so when you hit the up key, it will progressively make the ship move faster, and make it go progressively slower when hitting the down key. It also has to have a limited amount of speed. Here's my script:

 var Speed = 50;
 var Player = transform;     
 
 function Update() 
 {
     transform.Translate(Vector3.forward * Time.deltaTime * Speed);
     
     if(Input.GetKey("up"))
     Player.rigidbody.AddForce(transform.forward * 10);
     
     if(Input.GetKey("down"))
     Player.rigidbody.AddForce(transform.forward * -10);
 }

Several things are wrong with this. I'm getting the error message:

Screen position out of view frustum (screen pos 917.000000, 646.000000) Camera rect 0 0 1552 723)

...when I hit the up key or the down key. And I get this error message right after that:

!!sNormalized (dir)

so now I'm completely and utterly confused. If you could, that would be appreciated. Thanks.

Comment

People who like this

0 Show 1
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 DayyanSisson · Aug 16, 2011 at 03:22 PM 0
Share

What I mean by a limited amount of speed is that the player can speed up to a certain number, or slow down to a certain number.

4 Replies

  • Sort: 
avatar image
Best Answer

Answer by redlotus · Aug 17, 2011 at 02:07 AM

You need to add a few variables. Apologies if I don't get the syntax exactly right -- I'm more of a C# guy:

 var defaultSpeed = 50.0; // speed to default towards if no keys are pressed
 var maxSpeed = 75.0; 
 var minSpeed = 25.0;
 var maxAcceleration = 5.0; // Controls the acceleration
 var currentSpeed = 50.0;
 
 // var Player = transform;  // this line gave me an error and it isn't needed any way
 
 function Update() 
 {
     transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
 
     if(Input.GetKey("up"))
     currentSpeed += maxAcceleration * Time.deltaTime;
     else if(Input.GetKey("down"))
     currentSpeed-=maxAcceleration * Time.deltaTime;
     else if (currentSpeed > defaultSpeed){
         currentSpeed -= maxAcceleration * Time.deltaTime;
         currentSpeed = Mathf.Clamp(currentSpeed, defaultSpeed, maxSpeed);
     }
     else {
         currentSpeed += maxAcceleration * Time.deltaTime;
         currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, defaultSpeed);
     }
     currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
 }

A little sloppy, but I believe that this is what you're asking for.

Comment
Bravini
slayer29179
kishan17

People who like this

3 Show 3 · 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 DayyanSisson · Aug 17, 2011 at 05:33 AM 0
Share

Thank you. This almost works. There are no errors (so your syntax is fine), and I can lower the speed by a certain amount, but I can't speed up. Plus, no matter how much I change the speed variables, nothing changes. I'm not sure why.

avatar image redlotus · Aug 17, 2011 at 01:11 PM 0
Share

Sorry, but I forgot to make all of the variables floats. I've edited the script and you should be able to speed up/slow down correctly.

As for changing the speed variables, are you changing them in the inspector or the script?

avatar image DayyanSisson · Aug 17, 2011 at 04:48 PM 0
Share

Yeah, my bad, I was fixing them in the script. You're right. I'm sorry. Thank you. Perfect, it works.

avatar image

Answer by FTheCloud · Aug 15, 2011 at 05:34 PM

Using Translate and AddForce are two completely different parts of the Unity engine. One uses the physics engine and the other uses the transform engine. You can get the same effect but there not interchangeable.

You've got to re-write your script so when you press the up key it simply increases the variable "speed" and when you press the down key it decreases the variable "speed".

var Speed : int = 50; var FastSpeed : int = 75; var SlowSpeed : int = 25; var Player = transform;

function Update() { transform.Translate(Vector3.forward Time.deltaTime Speed);

 if (Input.GetKeyDown ("up")){
 Speed = FastSpeed;

} if(Input.GetKeyDown ("down")){ Speed = SlowSpeed; } if (Input.GetKeyUp ("up")){ FastSpeed = Speed; } if (Input.GetKeyUp ("down")){ SlowSpeed = Speed; } }

Comment
Rennat

People who like this

1 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 DayyanSisson · Aug 16, 2011 at 03:20 PM 0
Share

Well, this answered one of my other questions, how to boost. Ha. Well thanks because now my ship has a boost, but I need the speed to slowly increase, not have fixed speeds. Thanks anyway.

avatar image

Answer by DGArtistsInc · Aug 15, 2011 at 05:30 PM

ok i have worked on a game like this before and here is how i did it.

 var curSpeed = 10.0;

 function Update()

 {

    rigidbody.AddForce(transform.forward * Time.deltaTime * curSpeed);

    if(Input.GetKey("up"))

    {

        curSpeed += 1;

    }

    if(Input.GetKey("down"))

    {

        curSpeed -= 1;

    }

 }
Comment

People who like this

0 Show 4 · 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 FTheCloud · Aug 15, 2011 at 05:45 PM 0
Share

The problem with this script is it doesn't reset the speed to what it was before the user pressed the key and every time you press up or down it constantly gets faster and faster and vice versa.

avatar image DayyanSisson · Aug 15, 2011 at 06:31 PM 0
Share

Now, when the game starts, I don't have to press anything. The screen doesn't show anything and I get this error message:

Screen position out of view frustum (screen pos 906.000000, 687.000000) Camera rect 0 0 1428 717)

avatar image DGArtistsInc · Aug 15, 2011 at 08:40 PM 0
Share

nighthawx349 are you putting this script on the camera or your ship?

avatar image DayyanSisson · Aug 15, 2011 at 09:15 PM 0
Share

On my ship

avatar image

Answer by Pwest01 · Apr 03, 2012 at 11:51 PM

I understand this is an older thread, but wanted to add a few comments (my two cents). Variables are not that expensive, and good code should use them if it makes the script more versatile. For instance, having a max speed and min speed variable (min speed = 0 or less if you want to fly backward). This way as the player presses forward, the currentSpeed variable can move within the constraints of min and max speed. Also, a variable called lastSpeed can be used to save currentSpeed when a player triggers boost. This way when the boost is over the speed can adjust back to the value it held previous to the boost. You asked about gradual increase and decrease in speed. This can be accomplished using the Mathf.Lerp().

Are you also transforming your camera position with the ship movement?

Comment

People who like this

0 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 DayyanSisson · Apr 04, 2012 at 12:53 AM 0
Share

Yes, the camera is transforming with the ship. And yes thanks for that. I haven't looked into Mathf.Lerp() completely. What does it do exactly?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with 2D Flight Simulator 0 Answers

Flight HUD - flight angles 0 Answers

fly speed , increasing speed 1 Answer

Flight physics 0 Answers

How to move the player farward at a constant speed on Z-axis 2 Answers


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