Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Dragonic TV · Aug 05, 2015 at 10:33 AM · javascripttransformpositionsmoothdeltatime

Transform.position can't be changed smoothly.

Hello UnityAnswers, I have a problem. Not sure if small or huge tho! I have a code that allows an object to change it's position.x when you swipe to a direction. The problem that i have here is, I can't make it work to go from position.x = 0 to position.x = 4 smoothly. For example, when you swipe right, "Player" instantly goes to position.x = 4. I tried the code : this.transform.position.x = 4 * Time.deltaTime * 2; But nothing happens. Here is my full code written in JS.

 #pragma strict
 
 var firstPressPos : Vector2;
 var currentSwipe : Vector2;
 var current_pos : int;
 var speed : int;
 var can_speed : boolean;
 
 function Start () {
     current_pos = 2;
     speed = 3;
     speed_up();
 }
 
 function Update () {
     this.transform.position.z = this.transform.position.z + speed * Time.deltaTime * 2;
 
 
     if (Input.GetMouseButtonDown(0))
     {
         firstPressPos = Input.mousePosition;
     }
     if (Input.GetMouseButtonUp(0))
     {
         currentSwipe = Input.mousePosition - firstPressPos;
 
         if (currentSwipe[0] + 150 > 0)
         {   
             if(current_pos == 3){
                 return;
             }if(current_pos == 2){
                 this.transform.position.x = 4;
                 current_pos = 3;}
             if(current_pos == 1){
                 this.transform.position.x = 0;
                 current_pos = 2;}
         }
 
         if (currentSwipe[0] + 150 < 0)
         {   
             if(current_pos == 1){
                 return;
             }
             if(current_pos == 2){
                 this.transform.position.x = -4;
                 current_pos = 1;}
             if(current_pos == 3){
                 this.transform.position.x = 0;
                 current_pos = 2;}
         }
         }
     }
 
 function speed_up(){
     for(var i = 0; i  < 10; i++){
         yield WaitForSeconds (10);
         if(speed < 21){
             speed = speed + 6 ;
             InvokeRepeating("speed_up", 0, 2);
             can_speed = false;}else{return;}
     }
 }

Thanks for your time!

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
0

Answer by Hexer · Aug 05, 2015 at 10:39 AM

You could make use of Vector3.MoveTowards.

http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Or you could make a small dash animation that gives the illusion that the player moved with an incredible high speed.

I find the latter cooler, but it is up to you.

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 $$anonymous$$ · Aug 05, 2015 at 01:31 PM

You can use vector3.lerp for smooth movement.

http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

This may help you.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Slower Ladder 2 Answers

How to smooth movement object in one-axis? 1 Answer

Smooth Rotation Help 1 Answer

How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer

Change object position on trigger enter 3 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