Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 17yaot1 · Oct 16, 2016 at 10:08 AM · time.deltatimefunctionstransform.translate

Why can't I put transform.translate into a function in unity? (javascript)

I am trying to create a function which issues specific transform.translate commands to move game pieces.

it goes something along the lines of:

 static var MoveUpleft = function () {
   if (Time.deltaTime < 1) {
     transform.Translate(x * Time.deltaTime,y * Time.deltaTime,z * Time.deltaTime,Space.World)
   }
 };

and a logic system like:

 static var MoveUpLeft = false;
 
 if (condition == true) {
   MoveUpLeft = true
 }

and on the thing that needs to move:

 function Update() {
   if (MoveUpLeft == true){
     if (Time.deltaTime < 1) {
       MoveUpleft();
     }
   MoveUpLeft = false;
   }
 }



The problem that I am getting is that there is a compiler error (meaning that there is already a problem before the game can even be run) , saying that "transform" is an unknown identifier. I ran a test script with transform.translate directly under update, and not by use of the function, and the problem went away. Does this mean that you cannot use transform.Translate in a function?

How about other alternatives for motion, like addForce?

This problem has been bugging me for days now, so thanks in advance!

I am complete beginner in using unity, or coding in general, as I only knew about unity for around half a year, and haven't devoted much time into it, so sorry if this question sounds stupid!

As an additional note, can Time.deltaTime be put in an if statement like that? Does it reset every time it gets called? How does it work?

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
Best Answer

Answer by doublemax · Oct 16, 2016 at 10:14 AM

 static var MoveUpleft = function () {

A static method is not connected to any class instance, so there is no "transform" available. Do you really need/want a static method here? Probably not.

As an additional note, can Time.deltaTime be put in an if statement like that? Does it reset every time it gets called? How does it work?

Time.deltaTime is the time since the previous frame was rendered. It's updated every frame.

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 tanoshimi · Oct 16, 2016 at 10:19 AM 2
Share

If the OP does want a static method (which, I agree, is unlikely), they could always pass the Transform component in question as a parameter to the $$anonymous$$oveUpLeft method:

 static var $$anonymous$$oveUpleft = function ( transform : Transform) {
   transform.Translate(x * Time.deltaTime,y * Time.deltaTime,z * Time.deltaTime,Space.World)
 }
avatar image 17yaot1 · Oct 16, 2016 at 01:15 PM 0
Share

I tried making the function not static, but the same complier error
"BCE0005: $$anonymous$$ identifier: 'transform'." occurs.

This is my exact code:

public class directions {

static public var $$anonymous$$ovecounter = 0;

var right : float = Time.deltaTime 1.602389; var up : float = Time.deltaTime 2.77544987; var left : float = Time.deltaTime -1.602389; var down : float = Time.deltaTime -2.77544987;

var $$anonymous$$oveUpright = function () {

transform.Translate( right, up, 0 , Space.World);

};

Sidenote: I have mistaken what Time.deltaTime was. Thank you for infor$$anonymous$$g me :)! What I wanted was a Time.time that could reset, and I just assumed Time.deltaTime was Time.time without research.

However, the method suggested by tanoshimi seems to work. Thanks!

Again, thanks for the quick replies!

avatar image Bunny83 17yaot1 · Oct 16, 2016 at 03:14 PM 0
Share

Why do you actually create an anonymous method? That's the main problem here. Also you initialize it in a field initializer. Field initializer are executed even before the constructor that's why this can't never work, no matter if it's a static or an instance variable.

Just create a normal class method:

 function $$anonymous$$oveUpright()
 {
     transform.Translate( ... );
 }

You can't use Time.deltaTime in a field initializer as well. Also it wouldn't make any sense. DeltaTime might be different each frame and that's actually the main reason why you would use it at all. If you just assign the value once it would be a constant.

This wouldn't make much sense in an actual Javascript environment either. $$anonymous$$eep in $$anonymous$$d that Unity doesn't use Javascript. Unityscript is just a .NET language that has a syntax similar to Javascript but in fact has nothing to do with Javascript. It's a completely different language.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

different devices -> Different touch speed 3 Answers

how do i make this to a negative (ShopRespawn < 180)? 1 Answer

Gameobject moving faster at higher framerates? Using Time.deltaTime 2 Answers

Time class == null in Unity 4.3 1 Answer

How to AddForce OverTime? 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