- Home /
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);
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.
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
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)
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.
Thank you, Sir!
That was totally the problem - much appreciated. All working great now.