• 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
1
Question by David Gutierrez · Sep 01, 2010 at 01:33 PM · 2drotation

Transform.forward is always (0,0,1)

Hey guys, I'm trying to put together a Geometry Wars like clone just to become familiar with Unity. Currently I have an orthographic camera set up facing into the positive Z axis. I also have a cube, which is the player, moving around the screen and always rotating to face the mouse position.

Now, I've been trying to instantiate a bullet prefab in front of the player with a movement direction in the same direction the player is facing at the point of the instantiation. I've tried to simply Instantiate the prefab using...

Instantiate(bullet, transform.TransformPoint(transform.forward * 20, transform.rotation);

But Unfortunately, no matter how my cube is rotated transform.forward is always (0,0,1). And since my camera is orthogonal and facing into the positive Z axis, the bullet just appears right at the player position.

I have similar problems moving the actual bullet in the direction the player was facing at the time of instantiation. Doing...

transform.Translate( transform.forward * bulletSpeed * Time.deltaTime );

Has no effect either since transform.forward is (0,0,1) here as well.

So my question is, what should I be doing in order to get transform.forward to properly reflect the ships rotation. Or should I be using something else entirely?

Thanks!

Comment
Add comment · Show 1
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 matyicsapo · Sep 01, 2010 at 03:20 PM 0
Share

really am not sure if it works but i'd try this - http://unity3d.com/support/documentation/ScriptReference/Transform.InverseTransformPoint.html or better sounding - http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$atrix4x4.$$anonymous$$ultiplyVector.html using this matrix http://unity3d.com/support/documentation/ScriptReference/Transform-worldToLocal$$anonymous$$atrix.html and transform.forward as the Vector3

2 Replies

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

Answer by David Gutierrez · Sep 01, 2010 at 06:53 PM

Okay, so I (thinK) I've partially nailed down the issue. For one, I was doing my cube rotation incorrectly. Rather then using Transform.LookAt() I was calculating the angle between the mouse and cube manually using the arc tangent of the delta Y over delta X. But this actually was only rotating my cube 180 degrees, and then flipping it to go another 180. I hadn't noticed this issue because I had a solid color material on the cube, so any flipping wasn't visibly noticeable.

So I rewrote my rotate script to use LookAt() here it is:

// Update is called once per frame void Update () {

 Vector3 mouseScreenPos = Input.mousePosition;
 Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos);

 mouseWorldPos.z = transform.position.z;

 transform.LookAt(mouseWorldPos,Vector3.forward);            

}

And, the cube seems to be rotating a full 360 without flipping around in between.

So the Transform.forward variable seems to be working correctly, because if I instantiate like so:

Transform tempBullet;
Vector3 inFront = transform.rotation * (Vector3.forward * 1);           
tempBullet = (Transform)Instantiate
             (bulletBody,inFront + transform.position, new Quaternion(0,0,0,0) );

The bullet prefab correctly spawns in front of the player! So that's half of the issue now, my other issue is getting the bullet to actually move forward in the direction the player was facing when the bullet was fired.

I tried setting the forward vector of the bullet manually, like so:

Vector3 bulletForward = transform.rotation * (Vector3.right);   
tempBullet.forward = bulletForward;

But this produces inconsistent behavior depending on the direction of the player.

Can anyone point me in the right direction for my next hurdle, which is: How do a get a newly instantiated prefab to move in the direction of the player from the moment of instantiation?

Thanks.

EDIT:

So I fixed it by changing the bulletForward to be as so:

Vector3 bulletForward = transform.localRotation * new Vector3(0,1,1);

Comment
Add comment · Show 2 · 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 Daniel-Brauer · Sep 01, 2010 at 07:07 PM 1
Share

Don't make your own Quaternion. 0,0,0,0 is not normalized, and thus not a valid rotation. If you want "no rotation", use Quaternion.identity.

avatar image David Gutierrez · Sep 01, 2010 at 07:10 PM 0
Share

I see! Thanks, I'll do that.

avatar image
1

Answer by Eric5h5 · Sep 01, 2010 at 06:47 PM

You just want transform.forward. transform.TransformPoint() is for converting local space to world space, which is pointless when you're using transform.forward to begin with.

Instantiate(bullet, transform.forward * 20, transform.rotation);
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 David Gutierrez · Sep 01, 2010 at 06:54 PM 0
Share

Oh my, in the 6 $$anonymous$$utes it took me to right my answer below you answered! Perhaps I'll revert my code changes mentioned in my answer and give this a try.

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

No one has followed this question yet.

Related Questions

Rotating a 2D object when the phone is rotated 1 Answer

Spin a 2D object with mouse drag 2 Answers

Problem with rotation and forward motion 2D Z-axis up 1 Answer

Rotating my character only while moving in a 2D plane 3 Answers

2D head breaks when flipping character 0 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