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 bart1ey · Jan 23, 2013 at 04:19 PM · c#movementcharactercontrol

C# Movement Script

I converted this small script from JavaScript to C# and I cant seem to get the Transform.TransformDirection working. I get this error

A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)'

I have tried different variations as well as instantiating it before creating the vector but no go.

 using UnityEngine;
 using System.Collections;
 
 public class characterController3D1 : MonoBehaviour {
     
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     public float slopeLimit = 45.0f;
     
     public Vector3 vertical = Transform.TransformDirection(Vector3. forward);
     public Vector3 horizontal = Transform.TransformDirection(Vector3.right);
     
     
     public void movement() {
         
         CharacterController controller = GetComponent<CharacterController>();
         
         if(Input.GetAxis("Verticle") || Input.GetAxis("Horizontal")){
             controller.Move ((vertical * (walkSpeed * Input.GetAxis ("Vertical"))) * Time.deltaTime);
             controller.Move ((horizontal * (walkSpeed * Input.GetAxis ("Horizontal"))) * 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

3 Replies

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

Answer by ragnaros100 · Jan 23, 2013 at 04:32 PM

You have to move your variable definitions down in void Start(). like this:

 ...
 public Vector3 vertical;
 
 void Start()
 {
    vertical = transform.TransformDirection(Vector3.forward);
 }
 ...
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 Loius · Jan 23, 2013 at 04:42 PM 0
Share

and this is because transform (and other behaviour properties) are undefined during the initialization process (all code outside functions) of an object.

avatar image
0

Answer by CheeseGoat · Jan 23, 2013 at 04:53 PM

     public Vector3 vertical = Transform.TransformDirection(Vector3.forward);
     public Vector3 horizontal = Transform.TransformDirection(Vector3.right);

Change the above to:

     public Vector3 vertical;
     public Vector3 horizontal;

then in Start() you can set them if you use a lower case t (which means the transform attached to this script)

 vertical = transform.TransformDirection(Vector3. forward);
 horizontal = transform.TransformDirection(Vector3.right);
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
avatar image
0

Answer by vbbartlett · Jan 23, 2013 at 04:35 PM

This is because TransformDirection is not a static function but rather a member function. This means that you call it on a specific transfrom.

See the Unity Reference on this

In your case I am betting that if you change like this it will work.

 public Vector3 vertical = transform.TransformDirection(Vector3. forward);
 public Vector3 horizontal = transform.TransformDirection(Vector3.right);

all that has been done is change the Transform to transform, this changes for a class static function to the instance of the gameObject's transform.

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

14 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Player slows down when hitting an incline 0 Answers

Change character's Y rotation based on velocity. 1 Answer

[3D] Top-Down Make Character move in the direction of the position he is currently looking at? 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