• 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 KarrenJ1989 · Aug 22, 2012 at 07:02 PM · cameraspeedrtsslerp

Slerping in movement speed?

I been reading a bit about slerping, and I'm not sure I can use it in my javascript, or exactly how it's working for calculations. I'm using a script for camera movement, RTS, and when you move the camera to the defined boundries, it starts scrolling, at the speed of a given variable [scrollSpeed], but this feels jerky, it takes you to full speed instantly, and it's hard to do precise movement. Is there a way to put like a function onit, so it starts out slow, and then builds in speed, til reaching the defined speed?

 myVector = (Vector3(mySpeed,0,0) * scrollSpeed * Time.deltaTime);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Aug 23, 2012 at 08:07 PM

using UnityEngine;
using System.Collections;
public class SmoothMover : MonoBehaviour
{
	//animation curve is nice for any y=f(x) to use in gameplay
	//use smooth function here. check it in inspector for graphic representation
	public AnimationCurve MoveSpeed = new AnimationCurve(new Keyframe[] 
	{ 
		new Keyframe(0.0f, 0.0f, 0.0f, 0.0f),
		new Keyframe(0.3f, 0.1f, 1.0f, 1.0f),
		new Keyframe(0.5f, 0.5f, 2.0f, 2.0f),
		new Keyframe(0.7f, 0.9f, 1.0f, 1.0f),
		new Keyframe(1.0f, 1.0f, 0.0f, 0.0f) 
	});
	//coordinates where we move from and to
	public Vector3 MoveFrom = Vector3.zero;
	public Vector3 MoveTo = Vector3.right * 10f;
	//object that we move
	Transform movingObj;
	void Start()
	{
		//simple create here a primitive to check how it works
		movingObj = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
	}
	void Update()
	{
		//the main line of code 8)
		//Vector3.Lerp gives us a point between two points on needed offset in 0..1 range
		//MoveSpeed.Evaluate returns a y=f(x) from smooth function
		//Mathf.PingPong simple returns x that
		//changes in time from 0 to 1, then to 0, then to 1 etc
		movingObj.position = Vector3.Lerp(MoveFrom, MoveTo, MoveSpeed.Evaluate(Mathf.PingPong(Time.time * 0.5f, 1f)));
	}
}

links

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html http://docs.unity3d.com/Documentation/Components/animeditor-AnimationCurves.html http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.html http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.Evaluate.html http://docs.unity3d.com/Documentation/ScriptReference/Mathf.PingPong.html

Comment
Add comment · Show 3 · 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 ScroodgeM · Aug 23, 2012 at 08:08 PM 0
Share

one more way to use cosine function in 0-180 degrees range. cosine changes in 1...-1 range smoothly at start and end but fast in middle.

avatar image lvictorino · Aug 23, 2012 at 08:13 PM 0
Share

@Scroodge I think it would help him understand your code with some comments in it :/

avatar image ScroodgeM · Aug 23, 2012 at 08:43 PM 0
Share

added some 8) sorry 8)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RTS camera 1 Answer

Making an RTS camera 1 Answer

Pan RTS camera controller without changing elevation/zoom 0 Answers

Quaterion.Slerp + Lookat, camera flipping 0 Answers

Transition current camera rotation to 0,0,0 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