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 0tacun · Jul 25, 2013 at 12:19 AM · movementtimecontroldash

Movement over Time

Hello, I'm new to Unity, and I'm trying to do a side-stepping / dashing movement for my character.

     var startTime : float;
     var duration : float = 3.0;
     var x : float;
     
     if(Input.GetButtonUp("Shift"))
     {
         if(combat_active == true)
         {
             if(Input.GetAxis("Vertical") > 0 && !animation.IsPlaying("combat_stance_swift_fwd"))
             {
                 startTime = Time.deltaTime;
                 if(Time.deltaTime - startTime > duration)
                 {
                     Debug.Log("Dashing forward");
                     animation.CrossFade("combat_stance_swift_fwd");
                     move_direction = Vector3(0,0,1);
                     move_direction = transform.TransformDirection(move_direction);
                     //if(animation)
                     movement_block = true; // disable further x,z input movement
                         
                     controller.Move(move_direction);
                     
                 }
             }
 }

this code block stands in the Update() function. And doesn't work.

I'm a bit confused how to make the transition from one positon to the forward position smooth. All I manage to do is a teleportation forwards.

I also tried if and for, but i didn't got the results. Can someone please explain how to do a movement in a timeframe?

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 Benproductions1 · Jul 25, 2013 at 01:20 AM

Hello,

Just looking at the movement code, I can already see many problems.

1 it will never execute

if startTime is set to Time.deltaTime before every check wether Time.deltaTime - startTime > duration,
then Time.deltaTime - startTime will always equal 0 and will never be over duration (or always under)

2 You are not taking into account time

Update is performed every frame. If every frame you move by a set amount, then firstly, your speed (distance over time) will vary depending on yoru framerate and secondly with the amoutn of movement you've coded in, you will move at the speed of sound at 300 fps.
If you want to make it framerate independant, you need to "find" the amount of time that has passed since the last frame.
Unity includes this functionality in it's Time class, called deltaTime.

 var velocity:Vector3 = Vector3.foward;
 
 function Update() {
     transform.position += velocity; //distance per frame
 
     transform.position += velocity*Time.deltaTime; //distance over time
 }

Hope this helps,
Benproductions1

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 0tacun · Jul 25, 2013 at 01:07 PM

Thank you very much for your hints!

I rethought my code and changed it. It is still ugly, but at the moment it does what i want.

 if(Input.GetButtonUp("Shift") || dashing == true)
     {
         if(combat_active == true)
         {
             if( Input.GetAxis("Vertical") > 0)
             {
                 dashing = true;
                 
                 
                 if(timer > 0)
                 {
                     
                     Debug.Log("Dashing forward");
                     animation.CrossFade("combat_stance_swift_fwd");
                     move_direction = Vector3(0,0,2);
                     move_direction = transform.TransformDirection(move_direction);
                     movement_block = true; // disable further x,z input movement
                         
                     controller.Move(move_direction*Time.deltaTime);
                     
                     timer -= Time.deltaTime;
                     counter++;
                     Debug.Log(timer);
                 }
                 else if(timer < 0)
                 {
                     dashing = false;
                     timer = animation["combat_stance_swift_fwd"].length/2;
                 }
             }

}

It isn't the best solution, so now I'm going to try to use subfunctions for the dashing movement.

Thank you again for your help!

0tacun

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

16 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Help with my Dash move? 1 Answer

How long do movement controls take? 2 Answers

Is it a bad practice to change the timescale for the whole game? 1 Answer

On screen movement control? 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