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 /
This question was closed May 23, 2012 at 11:18 AM by aldonaletto for the following reason:

Question answered on http://answers.unity3d.com/questions/255593/child-object-using-origin-0-0-0-as-forward-directi.html

avatar image
0
Question by asimov · May 18, 2012 at 12:56 AM · triggershootingaimmouse-draglockon

Shooting forwards again after locking onto enemy position

Hi,

I have an issue with shooting straight ahead again after the aim has previously been locked onto an enemy. My set up is I have the Player that moves along the Z-axis that has a long box trigger extending ahead of him along the Z-axis. This acts like a crude auto-aim, where any enemies entering/touching the the trigger are added to an array and sorted into distance from from the Player. The closest of which is selected as the target. When a target has been set, a bool variable within the script is set to true. The script also calls another script which draws a crosshair over the target enemy's position.

Now, the script I believe I'm having the issue with is called Shooter_R which shoots the bullets at the target enemy positions. This script is attached to an empty object which is a child of the Player (the Player's right hand to be exact). The aiming onto the enemies works fine, but I am having an issue with aiming ahead once I have passed (that is, in the Z-axis) the enemy. I have done some debugging and know that there is no enemy still selected at this point. I seem to be having trouble trying to get the BulletSpawner_R to point forward (along the Z).

The symptoms are as follows -

  1. It shoots straight ahead to begin with, then if I move in front of an enemy to get the aim lock, it changes the bullet direction as planned. Then, if I continue without aiming on another enemy, the spawner seems to gradually rotate backwards by 180 degrees around the X-axis, so I end up shooting backwards.

  2. If I allow the Player to translate along the Z without shooting/aiming, it continues to shoot straight ahead.

  3. If I allow the Player to translate along the Z whilst shooting but not aiming onto any enemies, the same behaviour occurs (spawner rotates backwards).

One other thing to note is that the spawner does not move when looking at it in the Scene view. However, the rotation values in its Transform component change, which seems to be on par with the rotation seen in the Game view. The spawner also has its Z-axis pointing forward along the Z-axis (Globally). The same symptoms occur when the Local value is pointing either forward along the Z, or backwards in the opposite direction.

The Shooter_R script is pasted below. Feel free to ask any other questions if you have any.

Any help would be fantastic. Thanks

     using UnityEngine;
     using System.Collections;
     
     /* Note, this script is currently attached to the BulletSpawner_R Empty object */
     
     public class Shooter_R : MonoBehaviour
     {
         // Public variables are also shown in the Unity UI and can be changed in the Inspector
         public Rigidbody bulletPrefab;       // holds a reference to the bullet Rigidbody
     //  public Rigidbody bullCasingPrefab; // holds a reference to the bulletCasing Rigidbody
         public float bulletPower = 1500.0f;  // used to set the power of the bullets
         public float moveSpeed = 2.0f;    // used to define the speed of movement of the camera using the arrow keys
     
         // The general variable to hold the direction of the bullt - Enemy or Forward
         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;
     
         // Use this for initialization
         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 = new Vector3(0, 0, 1);
         }
     
         // Update is called once per frame
         void FixedUpdate()
         {
             Firing();
         }
     
         void Firing()
         {
             /* Create a reference to the direction we wish to shoot in. The camera is facing the brick wall so it makes sense 
              to shoot objects at the wall in the camera's forward direction.
              We have reated a Vector3 type variable called fwd, and told it to represent the forward direction of the current
              transform this script is attached to (the camera's). */
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
     
             // Access the ClosestEnemy script and check whether an enemy has entered/touched
             // the trigger box volume, setting the EnemyTargetted bool to true.
             // if (PlayerObj.GetComponent<CrosshairToggle>().EnemyTargetted)
             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 (lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyTargetted == false)
             {
                 // Straight ahead
                 bulletDirection = fwd;
     
                 //bulletDirection = new Vector3 (0, 0, 1);
             }
             
             // Make the BulletSpawner_R object face the assigned direction
             transform.LookAt(bulletDirection);
     
     
             // Gain access to the Dragplayer script to check its playerDragged variable..
             bool playerTouched = playerObj.GetComponent<DragPlayer>().playerDragged;
     
             // If touching the screen with LMB AND touching the Player is true..
             if (Input.GetMouseButton(0) && playerTouched)
             {
                 Timer += Time.deltaTime;
     
                 if (Timer >= ShootDelay)
                 {
                     Timer = 0;
     
                     // Create a new variable named instance (of type Rigidbody - the bullet in this case) 
                     // Into this variable we are storing a reference to the creation of a new object that is of type Rigidbody (bullet).
                     // Requires three pieces of information namely, Instantiate(What to make, Where to make it, a rotation to give it);
                     Rigidbody newBullet = Instantiate(bulletPrefab, transform.position, transform.rotation) as Rigidbody;
                     
                     // Play the shooting sound
                     playerObj.GetComponent<PlayerSounds>().PlayShootSound();
     
                     //Rename the bullet instances to avoid the default '(Clone)' extension which Unity adds.
                     newBullet.name = "bullet";
     
                     /* We will apply the force by first referring to our variable that represents the newly created object—instance, 
                     then using the AddForce() command to add a force in the direction of the fwd variable—multiplied by the
                     public variable named power that we created earlier. */
     
     //              newBullet.AddForce(bulletDirection * bulletPower);
                     
                     newBullet.AddForce(transform.forward * bulletPower);
                 }
             }
         }
     
     }//ends class
Comment
Add comment · Show 3
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 02:15 AM 0
Share

Any suggestions on this?

Alternatively, can anyone recommend an established method of making a parented object face the forward / its original direction? I have a feeling there's something strange going on with Local/Global coordinates here.

Thanks

avatar image asimov · May 21, 2012 at 06:47 PM 0
Share

I finally figured out that the reason the Spawner seems to reverse its Z direction has nothing to do with the track, or the script, etc. It is simply using the Origin (0, 0, 0) as the forward direction. Therefore, as it approaches this point it begins to rotate, until it has rotated 180 degrees on the other side.

Now, can anyone lend any thoughts as to why this is happening and how to fix this?? :)

avatar image asimov · May 23, 2012 at 11:07 AM 0
Share

Just to let everyone know, this question was successfully answered here:

http://answers.unity3d.com/questions/255593/child-object-using-origin-0-0-0-as-forward-directi.html

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to detect if a GUI texture is over an object 1 Answer

Can't click gameobject when over another trigger? 1 Answer

I need help with aiming a ball. 2 Answers

extinguish fire 1 Answer

Use the mouse to aim? 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