• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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/Matrix4x4.MultiplyVector.html using this matrix http://unity3d.com/support/documentation/ScriptReference/Transform-worldToLocalMatrix.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 minutes 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Clamp rotation of sprite? 0 Answers

Camera Scrolling just track the Charakter if z-Rotation=180 0 Answers

Unity Car Tutorial Rotate Snippet Chaos 0 Answers

Character rotation and move to mouse click point with speed 0 Answers

Move a 2D "Chains" end-effector to a certain point via physics. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges