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
1
Question by Ochreous · Apr 25, 2013 at 07:44 PM · c#transformtranslate

C# Smoothing Out transform.Translate

Hi everyone, is there a way to smooth out transform.Translate? I have a script where a gameobject is moved +1 in the y axis transform.Translate(0,1,0); but it feels very jagged and unnatural.

Comment
Add comment · Show 1
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 numberkruncher · Apr 25, 2013 at 07:49 PM 0
Share

Lerp is a good way to smoothen movements, take a look at the following link for some examples: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by whydoidoit · Apr 25, 2013 at 07:48 PM

Have a target position rather than changing the transform.position directly and then use an update function to Lerp towards it - or SmoothDamp etc.

SmoothPosition.cs

    using UnityEngine;

    public class SmoothPosition : MonoBehaviour
    {
           public Vector3 targetPosition;
           public Quaternion targetRotation; //Optional of course
           public float smoothFactor = 2;

           void Update()
           {
                 transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothFactor);
                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * smoothFactor);
           }
    }


Then rather than doing transform.position = something:

      gameObject.GetComponent<SmoothPosition>().targetPosition = something;

Though preferably cache it to avoid too many GetComponent calls.

Comment
Add comment · Show 9 · 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 Ochreous · Apr 25, 2013 at 08:07 PM 0
Share

That smooths it way too much and my gameobject doesn't go anywhere.

avatar image whydoidoit · Apr 25, 2013 at 08:52 PM 0
Share

Did you adjust the smoothing factor?

avatar image Ochreous · Apr 25, 2013 at 09:17 PM 0
Share

Yes I did.There seems to be a sort of balance between the smoothing and no smoothing. So far I have seen that if I don't have smooth my gameobject is able to make it to it's destination but it is not very smooth as it's making its way.But if I have the smoothing the object doesn't make it to it's destination.

avatar image whydoidoit · Apr 25, 2013 at 09:20 PM 0
Share

Well that's odd - I guess it could take a while but should be visually there quickly

You could do

    transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, maxSpeed);

Or use a SmoothDamp

avatar image Ochreous · Apr 25, 2013 at 09:39 PM 0
Share

Nope, still doesn't work.

Show more comments
avatar image
0

Answer by edcarlo · Jan 09, 2014 at 03:03 AM

 // if youre using left/right key try this
 
 int speed = 10;
 transform.Translate(Vector3.left * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
 
 // Input.getaxis() value is 0.0 - 1, it accelerate to 1 if you pressed left/right key and decelerate if released the key.
 
 try to create variable and accelerate and limit its value to 1.
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 philipvella · Jan 09, 2014 at 07:43 AM

This worked great for me!

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 sath · Jan 09, 2014 at 08:18 AM

Try this

     public Transform target;
     public float speed = 20f;

     void Update() {
         float move = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, move);
     }


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

18 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

Related Questions

C# Children and SubChildren Null Reference Exception 1 Answer

C# locking Main Camera's Rotations 1 Answer

C# Need Help Refining Ricochet Script 1 Answer

C# Function Transform to Gameobject Convert 1 Answer

C# GameObjectList not Setting Parent 0 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