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 Chris 33 · Apr 01, 2011 at 06:36 PM · positionlerpsmooth

Help With Lerping, not larping

so i need to lerp an object between a position off and on camera. With the code below, it lerps off camera right away, then with i hit the space bar to move the camera on screen it jumps right to the on screen position, rather than smoothly lerping on camera. Sorry about the redundant and messy code, im a beginner. Thanks!

var theX : float; var theZ : float; var theMain : GameObject; var mini : GameObject; var theY : float = 1.187029; var newX : float; var newZ : float; var cur : Vector3; var pre : Vector3; newX = theX + 1.257045; newZ = theZ - 1.179323; cur = Vector3(4.839913, -0.5300512, newZ); pre = Vector3(newX, -0.5300512, newZ);

function Update () { theMain = GameObject.Find("Main"); mini = GameObject.Find("mini"); theX = theMain.transform.position.x; theZ = theMain.transform.position.z; var exe = Input.GetKey("space");

 if (exe)
 {
     transform.localPosition = Vector3.Lerp(pre, cur, Time.time);
     mini.camera.depth = -2.0;

 }
 if (!exe)
 {
     transform.localPosition = Vector3.Lerp(cur, pre, Time.time);
     mini.camera.depth = 1.0;
 }

}

Comment
Add comment · Show 4
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 PrimeDerektive · Apr 01, 2011 at 06:53 PM 0
Share

LOL @ the title, tell me that was you $$anonymous$$ike.

avatar image e-bonneville · Apr 01, 2011 at 07:11 PM 0
Share

I'll bet the OP named it "Help with larping", and $$anonymous$$ike fixed it. LOL.

avatar image PrimeDerektive · Apr 01, 2011 at 07:45 PM 0
Share

Hahaha yeah, thats what I suspected.

avatar image Mike 3 · Apr 01, 2011 at 10:37 PM 0
Share

I didn't, no, that was the original (humorous) title :D

2 Replies

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

Answer by Mike 3 · Apr 01, 2011 at 06:43 PM

You either need to store the current t value (i.e. 0->1 of how far you are into the lerp) and increment that by Time.deltaTime, or you can use Vector3.MoveTowards, and move from the current position to the wanted position, instead of start to end

I'd go with the latter for ease of reading:

if (exe) { transform.localPosition = Vector3.MoveTowards(transform.localPosition, cur, Time.deltaTime * speed); mini.camera.depth = -2.0;

} else { transform.localPosition = Vector3.MoveTowards(transform.localPosition, pre, Time.deltaTime * speed); mini.camera.depth = 1.0; }

The speed value is how fast it'll move in meters per second, and needs declaring elsewhere

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 Chris 33 · Apr 02, 2011 at 07:31 PM 0
Share

nailed it! The greatness of the unity answers community never fails me. Soooo good!

avatar image
0

Answer by Owen-Reynolds · Apr 02, 2011 at 02:22 AM

Lerp's second input is a percent, from 0 to 1, with anything more than 1 counting as 1. Time.time is the total seconds you've been running. So, all Time.time by itself is good for is the first second after Play. To get smooth motion with Lerp, you'd have to use Time minus time when you last pressed space, divided by how many seconds you want it to take. MoveTowards is easier.

Lerp can be used for a "fast at first, then slower" move. You run this every frame, and change newPos whenever you feel like it (the Camera scripts use this trick):

transform.position = Vector3.Lerp(transform.position, newPos, 0.03f);

The 0.03f (I use C#, so you may not need the 'f',) means to go 3% closer each frame. Making it smaller goes slower. Anything 0.1f of more (10% each frame) almost snaps it to newPos.

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

Smooth Camera/Object Movement 1 Answer

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

Lerp has some bumping effect 1 Answer

Smoothly moving object along 1 axis 1 Answer

How to smooth a single value over time? 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