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 asimov · May 21, 2012 at 08:58 PM · directionchildrenforwardz-axisorigin

Child object using Origin (0, 0, 0) as its forward direction

Hi all,

I have a child empty object which is supposed to face the forward direction. However, it does so as it moves towards the Origin (0, 0, 0) direction, then as it moves passed the Origin, it flips its Z-axis (by 180 deg) and faces in the opposite direction as it moves away.

For example, if starting on the left of the Origin, it will move forward fine (with its Z-axis pointing to the right), then flip its Z to face to the left as it passes it, and vice-versa if approaching from the right.

I'm using transform.forward in the script attached to the child object. I have also tried transform.TransformDirection(Vector3.forward), new Vector3 (0, 0, 1). I then make the object LookAt() this direction.

The relevant part of the script is below.

Any ideas why its doing this?

Thanks in advance.

     // Hold the direction the bullet will fire - Enemy or straight ahead
     public Vector3 bulletDirection = Vector3.zero;

     // Variable to hold the direction of the enemy, assigned to the Instantiation, etc
     private Vector3 enemyDirection = Vector3.zero;

     // Used to reference the Player object
     private GameObject playerObj;

     // Used to reference the LockOnTriggerObject
     private GameObject lockOnTriggerObj;

     public float ShootDelay = 1.0F;

     public float Timer = 0.0F;

     private Vector3 fwd = Vector3.zero;

     void Start()
     {
         // Find the Player Object 
         playerObj = GameObject.Find("Player");

         // Find the LockOnTrigger object
         lockOnTriggerObj = GameObject.Find("PlayerLockOnTrigger");

         // Direction is along the Z-axis - straight ahead
         fwd = transform.TransformDirection(Vector3.forward);
     }

     void FixedUpdate()
     {
         Firing();
     }

     void Firing()
     {
         // If enemy targeted, set bulletDirection to position of the enemy
         if (lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyTargetted)
         {
             Debug.Log("Inside if-");

             // Access the ClosestEnemy script and its EnemyPosition variable
             enemyDirection = lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyPosition;
             // Assign the value in the variable to the bullet's direction
             bulletDirection = enemyDirection;
         }
     
         // If no enemy targeted, shoot straight ahead
         if (lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyTargetted == false)
         {
             // Straight ahead - original direction
             // bulletDirection = fwd;
 
             // bulletDirection = gameObject.transform.TransformDirection(0, 0, -1)

             // transform.TransformDirection(Vector3.forward)
             
             bulletDirection = new Vector3 (0, 0, 1);
         }

         // Make the BulletSpawner_R object face the assigned direction
         transform.LookAt(bulletDirection);

         // Fire the bullet
         newBullet.AddForce(transform.forward * bulletPower);
  
Comment
Add comment · Show 7
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 Berenger · May 21, 2012 at 09:17 PM 0
Share

We're gonna need a bit of code.

avatar image asimov · May 21, 2012 at 09:31 PM 0
Share

Sure - have added the code.

avatar image whydoidoit · May 21, 2012 at 09:38 PM 0
Share

One thing, it looks like your code for the enemy direction bit is wrong, it should probably be taking the bullets position away from the enemyPosition in order to get a direction.

avatar image asimov · May 21, 2012 at 09:57 PM 0
Share

Thanks for the suggestion.

Have made a couple of changes to match the code I'm using at the moment. Well, the locking onto the enemy position seems to work and so does the returning to the forward position.

Doesn't LookAt() take care of the direction if you enter the position to look at?

Can you recommend any changes I could make as I'm not sure of established methods for doing this?

Think I need to find a way to tell a child object to always face a forward direction irrespective if where it is.

Thanks

avatar image whydoidoit · May 21, 2012 at 10:28 PM 0
Share

By forwards do you mean "world forwards"?

LookAt looks at an object - not in a direction - you are thinking of Quaterion.LookRotation I think - that would give you a rotation that looks in a direction. Something like transform.rotation = Quaternion.LookRotation(bulletDirection)

Show more comments

1 Reply

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

Answer by whydoidoit · May 21, 2012 at 10:34 PM

So I think this is it:

You need to change your code to do the following:

When you are targeting an enemy - take the current transform position away from the enemies position. Then use Quaternion.LookRotation to set the transforms rotation to point in that direction then add your force like you are now.

 bulletDirection = lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyPosition - transform.position; 
 ...
 transform.rotation = Quaternion.LookRotation(bulletDirection)

Then remove the transform.LookAt();

Now the code that fiddles with forward will work the same way as the code that targets the enemy. The problem you have is that at the moment they are both trying to do a different thing. One is using positions and the other directions.

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 asimov · May 21, 2012 at 11:07 PM 0
Share

Thank you, Sir!

That was totally the problem - much appreciated. All working great now.

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

How to add slight angle/rotation variation to transform.forward? 5 Answers

Change characters direction using camera view 0 Answers

Heat-seeking missile code. 1 Answer

Trying to figure out what direction an NPC are walking 1 Answer

Rigidbody Player AddForce issue 2 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