• 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 someGamer2 · Jun 24, 2017 at 02:21 AM · transform.forward

how to get the velocity Vector3 from global to local?

Hi, I've been searching for hours but cant find the answer i'm looking for.

I have an enemy that jumps, i want him to jump forward based on his own forward vector but when I try to convert it to local space it doesn't work he jumps based on the forward vector of the global space.

 Vector3 Jump()
     {
 
         float intialvY = 60;
         float intialVx = 500;//350
         gravity.y = -9.8f;
 
         if(jumpToSteal == true)
         {
           myTransform.position += velocity * moveSpeed * Time.fixedDeltaTime;
 
             float tempVel = myTransform.InverseTransformDirection (velocity).z
   
             tempVel = intialVx * Time.deltaTime;
             velocity.y = ((intialvY) - (-gravity.y * timeTaken * 10)) * 20 * Time.deltaTime;
 
         }
 
         return velocity;
     }

I tried to use inverseDirection to change the z axis from global to local but then he only jumps straight up and doesn't move forward. The jumping works fine its just the forward direction that needs fixing. I also tried "Velocity = myTransform.forward" But that doesn't work either which is weird because its what i am using to move the enemy in its forward direction in a different function. Thanks for the help!

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

Answer by someGamer2 · Jun 27, 2017 at 10:56 PM

Took a while but I figured it out, for any future readers who want to be able to jump forward on the local axis for a gameobject.

 float intialvY = 60;//60;
 float intialVx = 500;//350
 float horizontalJump;
 float verticalJump;
 void Jump()
 {
     gravity.y = -9.8
     if(jumpToSteal == true)
     {
             fallSpeed = .2f;

             velocity = myTransform.TransformDirection (new Vector3(0, verticalJump, horizontalJump));//this line solved the problem


         myTransform.position += velocity * fallSpeed * Time.fixedDeltaTime;
   
         timeTaken += Time.deltaTime;
         verticalJump = ((intialvY) - (-gravity.y * timeTaken * 11)) * 20 * Time.fixedDeltaTime;
 
         horizontalJump = intialVx * Time.fixedDeltaTime;
 
         velocity = new Vector3 (0, verticalJump, horizontalJump);
         Debug.Log (velocity.z);
     }
 }//end of Jump()
 
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 Rickyinjp · Jun 24, 2017 at 08:26 PM

So I think an easy way would be to use

 transform.forward
 

so if you want to move a certain speed in the direction you're facing, do

 rigidbody.velocity = speed*transform.forward; // If you're using the physics engine. This should be in FixedUpdate()
 
 
 transform.position += speed*transform.forward*Time.deltaTime;
 // The above is if you're not using physics engine but it's naughty. It's not recommended to directly change the transform position.
 
 
 // Use Translate instead if there's no physics engine. Translate uses local axes.
 transform.Translate(Vector3.forward*speed*Time.deltaTime);
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 DroidifyDevs · Jun 24, 2017 at 04:30 PM

Global vectors will start with Vector3; for example Vector3.forward. Local vectors will look like this: YourGameObject.transform.forward.

Take a look at this: https://docs.unity3d.com/ScriptReference/Transform-forward.html

So in your code, try in line 12 myTransform.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 someGamer2 · Jun 25, 2017 at 12:24 AM 0
Share

I tried that but now the model doesn't push forward when his jumps, he jumps straight.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

67 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fire bullet in camera direction problem 3 Answers

Is there any way to translate normalized vectors into angles? 1 Answer

Adapting Velocity-based Platformer Controls to Camera Forward 0 Answers

setting Transform.forward results in strange artifact 2 Answers

Eulerangles and Transform.forward... something's off? 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