• 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 khos85 · Jan 28, 2014 at 01:43 PM · erroraxismovesmooth

SmoothDamp not working?

Hi,

I am moving my object using: transform.position.z-=0.1; because it is set to kinematic... I am trying to make the movement of this object much smoother, searching the WWW I find http://docs.unity3d.com/Documentation/ScriptReference/Mathf.SmoothDamp.html

So I changed the code from that page to match my preferred z axis, see code below:

var target : Transform; var smoothTime = 0.3; var zVelocity = 0.0; var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime); transform.position = Vector3(transform.position.x, transform.position.y, newPosition);

I have this in FixedUpdate() .. When running the code I see the exception: NullReferenceException: Object reference not set to an instance of an object script+$windtimer$54+$.MoveNext () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/script.js:121)

It is failing on line: var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime);

Can anyone tell why this is failing? Or has anyone got some code (or could adapt my code) that might make it easier to smoothly move objects to a new location?

Hmmm, even if I use the unedited code from that Unity reference it give the same error?!

many thanks in advance, Kim

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 JoaoOliveira · Jan 28, 2014 at 02:17 PM 0
Share

Are you defining zVelocity in the method's scope? You need to define it in the class scope, like in the example given in the documentation.

avatar image khos85 · Jan 28, 2014 at 04:48 PM 0
Share

Hi JoaoOliveira, I tried moving var zVelocity = 0.0; to class level scope, that makes no difference at all.

In the error I notice it shows 54... that is then referring to the position methid/property for "(transform.position.z,", why would that fail? It should know about that I would have thought? Curious.

avatar image khos85 · Jan 28, 2014 at 04:49 PM 0
Share

Like I said before , even if I copy paste that code from the documentation page I see the same error.

I am able to use transform.position.z in various other places in the script/method so I am finding this difficult to believe, understand.

avatar image khos85 · Jan 28, 2014 at 05:00 PM 0
Share

I think the issue might be related top that $$anonymous$$athf.SmoothDamp is expecting float variables but I am using target.position.z; and transform.position.z;

But even if I define these as: var myval1 : float = target.position.z; var myval : float = transform.position.z;

It does not help, but then the error shows on line: var myval1 : float = target.position.z;

So it seems something to do with this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Boreale · Jan 28, 2014 at 05:43 PM

Well, considering that null ref is in line

var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime);

and you're running your script on gameObject, the only thing that can be null is target variable. Make sure 'target' is assigned to your script in inspector or in another script.

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 khos85 · Jan 28, 2014 at 06:55 PM 0
Share

That fixed it, newbie mistake I guess, I had var target : Transform; in the method, it wasn't showing in the editor.. and I did not think to assign an object to this. Thanks, working now!

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

20 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

Related Questions

Rotatearound turning with player as pivot error 0 Answers

How to move an object from point A to point B with one key press 3 Answers

how can i move the player relative to the x smoothly? 0 Answers

Shoot a bomb to mouse position smooth 1 Answer

Moving an object based on the position of another object 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges