- Home /
Question answered on http://answers.unity3d.com/questions/255593/child-object-using-origin-0-0-0-as-forward-directi.html
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 -
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.
If I allow the Player to translate along the Z without shooting/aiming, it continues to shoot straight ahead.
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
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
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?? :)
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
Follow this Question
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