- Home /
Why are my projectiles facing the wrong way?
I'm making a weapon for my FPS game which shoots darts, it took me ages find a tutorial for this that worked, but now I have another problem:
My projectiles are facing the wrong way, they're pointing down rather than forward, how do I fix this?
Here's the code:
using UnityEngine;
using System.Collections;
public class Launcher : MonoBehaviour {
public Rigidbody projectile; //Variable to hold our projectile object of type Rigidbody.
public short speed= 999; //Variable 'speed' which determines how fast our projectile will shoot.
void Start ()
{
}
void Update (){
if (Input.GetButtonDown("Fire1")) //If player clicks left mouse button.
{
Rigidbody instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.localRotation);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
}
}
}
Also, how do I destroy projectiles after a certain period of time, or if there's too many in the game?
I also want to make it so that once they're dropped, the player can pick them up again, how would I do that?
Your object uses transform.localRotation. You should consider the transform.forward of the root object.
Isn't forward a vector3 value? How would I put it in a quaternion slot? Or do I do something else entirely?
They're still pointing down when shooting... Well, thanks anyway.
transform is a Vector3, therefore :
Vector3.forward // gives a world-space value
transform.forward // gives a value from local-space perspective
Same for the other Vector3 class variables, try it out.
Looks like $$anonymous$$ad$$anonymous$$ike has the answer (upvoted), if your model is not facing forward, put it as a child of an empty gameObject, correct the alignment, then save the empty with the rotated model as a prefab, use that.
Answer by madmike6537 · Jan 05, 2013 at 02:52 AM
To my knowledge this has to with the model itself and it's hard to fix. The simple way to fix it is to drop it inside an empty game object and then adjust the rotation of the child object.
Also look in to the destroy command, it has a automatic command to destroy after so many seconds.
I'm confused. So you drop the prefab into the scene parenting it with an empty?
Parent the projectile with an empty and update it as the prefab after you get it so it's ai$$anonymous$$g the right way.
Drag a copy of the projectile into scene. Create empty game object. Drag projectile into the empty to child it. Adjust rotation until facing the right way based on the parent empty. Now save as the prefab.
That kind of works, but it points in the same direction regardless of where I shoot, even when I shoot the other way!
Hmm its got something to do with how you are moving it with velocity. I am not as familiar with that method. Just curious why not just use rigidbody.addforce?
Your answer
Follow this Question
Related Questions
Need help with scripting bug fix. pleease 0 Answers
OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer
C# GameObject is not detecting collision with Character Controller 2 Answers