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 ZroeX · May 16, 2011 at 08:36 AM · speedmovelimit

About move way and Speed limit....

hello,everyone. I creat a move script for my protagonist.

var MousePoAlpha : Vector2;

var MousePoOmega : Vector2;

static var moveYN : boolean = true;

function Update () {

if(transform.position.z != 0){ transform.position.z=0; }

if(moveYN==true){ transform.rotation = Quaternion.LookRotation(rigidbody.velocity); if(Input.GetMouseButtonDown(0)){ MousePoAlpha=Input.mousePosition; }

 if(Input.GetMouseButtonUp(0)){
     MousePoOmega=Input.mousePosition;
     rigidbody.velocity=Vector3(MousePoOmega.x-MousePoAlpha.x,MousePoOmega.y-MousePoAlpha.y,0)/5;
 }
 rigidbody.velocity=rigidbody.velocity*0.999; // Slow deceleration.

}

if(moveYN==false&&Input.GetMouseButtonUp(0)){ transform.rigidbody.velocity =transform.TransformDirection( Vector3( 0, 20, 0) ); }

}

This is a 2DGame. I drog my protagonist to a direction and it will go.

My teacher say this is not good way to move. He say we need affiliate a "time". But I don't kown where we need affiliate this "time".

And it have a problem when speed was too high... It will through other Object!(No collision)

Because this is a phone game so I'm not willing to consume too many resources. So I think limit speed is a good way.

Have any API can limit speed about velocity?

Thank you.

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

2 Replies

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

Answer by save · May 16, 2011 at 10:28 AM

Physics only compute at a certain speed, when velocities gets to high it cannot keep up with computing the colliders around that particular rigidbody.

In our game we use something similar to this to control maximum speed:

var ShipSpeed = 0.0; var Acceleration = 10; var MaxSpeed = 100;

 function Start() {
     rigidbody.drag = 3;
     rigidbody.angularDrag = 10;
 }

 function FixedUpdate() {
     ShipSpeed = ShipSpeed + Acceleration*Time.deltaTime;
     if(ShipSpeed >= MaxSpeed){ShipSpeed=MaxSpeed;}
     var gas : float = Input.GetAxis ("Accelerate") * ShipSpeed;
     rigidbody.AddRelativeForce (Vector3.forward * gas);
 }

and let the drag do the deceleration. It really depends on what kind of game it is tho. If you're dealing with physics it should be inside a FixedUpdate.

Comment
Add comment · 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 The_r0nin · May 16, 2011 at 11:48 AM 0
Share

But won't your code run differently on different machines? If my machince can run each FixedUpdate at 2ms and yours takes 4ms, the car will accelerate fast on my machine, even with FixedUpdate. I think you still need a Time.deltaTime if you want the "feel" of your game to be the same across platforms (i.e. acceleration at the same rate, etc.)...

avatar image save · May 16, 2011 at 01:09 PM 0
Share

Very good point, I have to look into it thanks. I've always assumed that FixedUpdate() was fixed to a static time to compute the same physics over all platforms.

avatar image save · May 17, 2011 at 11:58 PM 0
Share

Added time as a variable to the script. Remember to always use FixedUpdate when dealing with physics over time.

avatar image
1

Answer by cjmarsh · May 16, 2011 at 10:11 AM

I think the problem is that the velocity is being multiplied by a constant scalar every frame, resulting in variable acceleration (depending on frame rate). If you introduce a variable to represent the magnitude of change to your velocity and then multiply it by Time.deltaTime you will get an actual acceleration that should obey the laws of physics once more.

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

No one has followed this question yet.

Related Questions

Limiting rigidbody Speed. 5 Answers

Pathfinding with specific end-direction and turning speed limit 0 Answers

How to limit the force from player input without limiting speed 2 Answers

Cap rigid body move speed, without capping rotation speed 1 Answer

Limit Speed One 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