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

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

Smooth moving object 2 Answers

Vector3.SmoothDamp has some invalid arguments 0 Answers

Map level selection slide smoothly 3 Answers

UnityException: Input Axis Rotate2 is not setup. 0 Answers

Lerp a Object along X and Z axis only. 3 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