- Home /
Unity Rotations not working properly? Help....
Hey, everyone. I'm experiencing a rather annoying bug and I was hoping someone could offer some help because this just isn't making sense to me. The Ballistic script tells the bullet to move in its forward direction, which should be whichever way the gun is pointing. When the playerAttack scripts calls for the bullet to spawn, it sets the bullets position to that of bulletSpawn's transform, which is a child of the gun.
OnShootEnter is being properly called, bulletSpawn is properly assigned, and yet when the bullet spawns, instead of it facing bulletSpawn's forward, its facing Vector3.forward???
These two scripts are the only things affecting bullet's spawn, rotation, and direction. Everything looks in order to me. What am I missing????
PlayerAttack extends MonoBehaviour, handles bulletspawning when the correct input is given.
public class PlayerAttack : MonoBehaviour {
public GameObject bulletPrefab;
public Transform gun;
public Transform bulletSpawn;
public float damage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnShootEnter(){
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
//bullet.GetComponent<Ballistic> ().forward =;
Debug.Log("Player Forward: "+ transform.forward+", BulletSpawn Forward: "+bulletSpawn.forward+", Bullet Forward: "+ bullet.transform.forward);
}
}
Ballistic extends MonoBehaviour, handles bullet's physics.
public class Ballistic : MonoBehaviour {
public GameObject model;
public float range;
public float accuracy;
public float speed;
Vector3 dir = Vector3.zero;
// Use this for initialization
void Start () {
model.transform.SetParent (this.transform);
//Vector3 dir = new Vector3 (transform.forward.x + Random.Range (-accuracy, accuracy),
// transform.forward.y + Random.Range (-accuracy, accuracy),
// transform.forward.z + Random.Range (-accuracy, accuracy));
dir = transform.forward;
Debug.Log ("Bullet Forward: " + transform.forward+", Direction: "+dir.normalized);
}
// Update is called once per frame
void Update () {
transform.GetComponent<Rigidbody> ().velocity = dir.normalized * speed;
range -= Time.deltaTime;
if (range <= 0)
Destroy (this.gameObject);
}
void OnCollisionEnter(Collision obj){
Destroy (this.gameObject);
//Debug.Log ("Bullet hit "+ obj.gameObject.name);
}
}
SetParent
tends to reset transformations. Try this ins$$anonymous$$d:
model.transform.SetParent (this.transform, true);
To keep the position and rotation assigned in Instantiate
Your answer
Follow this Question
Related Questions
How to make any dice have a given face(int) face up? 1 Answer
Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer
Instantiated Object Rotating Strangely 0 Answers
Player Control. Roller Ball with Cube Acceleration. 1 Answer
Retina specific framerate drop on specific function 1 Answer