Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mbariscck · Dec 18, 2015 at 11:56 AM · movementmovement scriptcodepage

i want to set maximum speed

i want to set maximum speed. What is my fault ? My English very very bad, sorry..

 using UnityEngine;
 using System.Collections;
 
 public class platetet : MonoBehaviour {
     //Movement Speed
     public float speed = 2;
 
     //Body Max Speed
     public float maxSpeed = 50f;
 
     //Flap Force
     public float force = 100;
 
     //JetpackForce
     public float jetpackForce = 50;
 
     // Use this for initialization
     void Start () {
 
         //Hareket edeceği yer
         GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
 
     }
 
     // Update is called once per frame
     void Update ()
     {
         //Flap
         if (Input.GetKeyDown (KeyCode.Space)) {
             GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
         }
         for (var i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch (i).phase == TouchPhase.Began)
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
             foreach (Touch touch in Input.touches)
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jetpackForce);
             if(GetComponent<Rigidbody2D>().velocity.magnitude > maxSpeed)
             {
                 GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * maxSpeed;
             }
         }
     }
 }
Comment
Add comment · Show 2
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 Salanyel · Dec 18, 2015 at 01:20 PM 1
Share

Oh my !

Here is your code with the right indentation. Pls indent it the next time :

 void Update () { 
     //Flap 
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) 
     { 
         GetComponent ().AddForce (Vector2.up force);
     } 
 
     for (var i = 0; i < Input.touchCount; ++i) 
     { 
         if (Input.GetTouch (i).phase == TouchPhase.Began) 
             GetComponent ().AddForce (Vector2.up force);
         
         foreach (Touch touch in Input.touches) 
             GetComponent ().AddForce (Vector2.up jetpackForce);
          
         if(GetComponent().velocity.magnitude > maxSpeed) 
         { 
             GetComponent().velocity = GetComponent().velocity.normalized maxSpeed; 
         } 
     } 
 }
avatar image mbariscck Salanyel · Dec 18, 2015 at 03:33 PM 0
Share

thank you, i'm very new on this forum :)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Samdeman22 · Dec 18, 2015 at 01:34 PM

Well, there are numerous places where you have random spaces where there shouldn't be:

 Vector2.up force

What is your intention here?

On the line which I think you are trying to limit the velocity (the case where the velocity magnitude is more than the maxspeed) you actually have incorrect syntax, as well as the fact that I'm not sure you are accessing the velocity component correctly (you would need to get the gameobjects

 GetComponent().velocity = GetComponent().velocity.normalized maxSpeed; 

Not only this, but there are spelling mistakes in places. I think you might benefit from using an editor which highlights errors like this. You can get Visual Studio Community edition for free from here https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx

You might also benefit from following a tutorial that does similar things, creating simple movement etc. Take a look at brackeys on youtube as an example.

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 mbariscck · Dec 18, 2015 at 03:32 PM 0
Share

I'm sorry my code is here.. How can i do ?

 using UnityEngine;
 using System.Collections;
 
 public class platetet : $$anonymous$$onoBehaviour {
     //$$anonymous$$ovement Speed
     public float speed = 2;
 
     //Body $$anonymous$$ax Speed
     public float maxSpeed = 50f;
 
     //Flap Force
     public float force = 100;
 
     //JetpackForce
     public float jetpackForce = 50;
 
     // Use this for initialization
     void Start () {
 
         //Hareket edeceği yer
         GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
 
     }
 
     // Update is called once per frame
     void Update ()
     {
         //Flap
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
             GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
         }
         for (var i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch (i).phase == TouchPhase.Began)
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
             foreach (Touch touch in Input.touches)
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jetpackForce);
             if(GetComponent<Rigidbody2D>().velocity.magnitude > maxSpeed)
             {
                 GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * maxSpeed;
             }
         }
     }
 }

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

34 People are following this question.

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

Related Questions

Movement input problem? 1 Answer

Character Movement on Cylinder Surface 0 Answers

Space Shooter Movement Scrip not working.,Space Shooter Tutorial Moving The player Errors in parts of code containing Rigidbody.(Insert Velocity, Position, Rotation) 0 Answers

Isometric movement for MoveTowards 0 Answers

Stop movement on collision 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