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 /
avatar image
0
Question by Supermacka · May 03, 2018 at 07:30 PM · transform.positioninfinite runner

How to change the speed of "transform.position"?

Hello,

I have made a movement script for a 3lane runner type of game and I am stuck at how to make the transition between lanes slower. At the moment it is instant, it seems it is dependent by the runtime or frameupdate maybe because I am using "transform.position". I have tried using "Time.scaletime" but it effect the speed of everything except the transition/laneSwitching. I also found this which is similar, but i did not get it working (link text).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Controllers : MonoBehaviour
 {
     public float speed;
     public int laneCount = 0;
     public bool maxLaneLeft = false;
     public bool maxLaneRight = false;
 
     public Rigidbody rb;
 
     public void Start()
     {
     }
 
     public void Update()
     {
         rb.AddForce(new Vector3(0, 0, 1) * speed * Time.deltaTime);
         Movement();
     }
 
     public void Movement()
     {
 
 
 
         if (Input.GetKeyDown(KeyCode.A) && maxLaneLeft == false)
         {
             transform.position += new Vector3(-5, 0, 0);
             transform.eulerAngles = new Vector3(0, 0, 0);
 
             laneCount -= 1;
 
         }
         if (laneCount == -1 && maxLaneRight == false)
         {
             maxLaneLeft = true;
             maxLaneRight = false;
         }
         if (Input.GetKeyDown(KeyCode.D) && maxLaneRight == false)
         {
             transform.position += new Vector3(5, 0, 0);
             transform.eulerAngles = new Vector3(0, 0, 0);
         
 
             laneCount += 1;
         }
         if (laneCount == 1 && maxLaneLeft == false)
         {
             maxLaneLeft = false;
             maxLaneRight = true;
         }
         if (laneCount == 0)
         {
             maxLaneLeft = false;
             maxLaneRight = false;
         }
     }
 }


Thanks in advance <3

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
Best Answer

Answer by mafima · May 03, 2018 at 08:10 PM

enjoy. alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LaneMover : MonoBehaviour {
      public float startspeed=10;
      public int lane = 0;
      public int maxLaneLeft=-1,maxLaneRight = 1;
      public float stepsize = 5;
      float desiredxpos=0;
  
      public Rigidbody rb;
  
      public void Start()
      {
          rb.AddForce(new Vector3(0, 0, 1) * startspeed);
      }
  
      public void Update()
      {
          
          In();
          Movement();
      }
  
     void In(){
          if (Input.GetKeyDown(KeyCode.A) && desiredxpos > maxLaneLeft*stepsize)
          {
              desiredxpos -= stepsize;
              lane -= 1;
          }
          if (Input.GetKeyDown(KeyCode.D) && desiredxpos < maxLaneRight*stepsize)
          {
              desiredxpos += stepsize;
              lane += 1;
          }
      }
     
      public void Movement()
      {
          transform.position = new Vector3(Mathf.Lerp(transform.position.x,desiredxpos,0.1f),transform.position.y,transform.position.z);
      }
 }


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 Supermacka · May 04, 2018 at 08:25 PM 0
Share

Thank you very much! Because if I just copy it i wont learn anything, i'm going to look at the code and try to reacreate it...

avatar image
0

Answer by Kciwsolb · May 03, 2018 at 07:57 PM

You are directly setting transform.position when the user presses a key. This is literally moving the object from one position to another. That is why you cannot "slow" it down. If you wanted a smooth transition while still only requiring one key press, you would need to either start a coroutine that changes transform.position by a very small amount each frame, or call a method in Update that moves your transform by a small amount each Update when a condition is true. The link you provided has a great example of how you could write a coroutine to do something like this in the comment of the accepted answer.

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

83 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 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 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

Transform.position.x new vector3 NOT changing object position 1 Answer

Pipe in a Snowboarding Infinite Runner not staying connected 0 Answers

Rigidbody2d.velocity making character stop in between 1 Answer

Transform Object over Players head before destroying 0 Answers

GameObject change Position after game started 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