- Home /
How do I reference an object's rotation through an attached script?
I feel foolish, but I can't find an answer to this anywhere. I have a first person camera from which I am launching a projectile. The following script is attached to it. When the projectile launches, it always points "North"; it always launches in the same direction and ignores my camera's orientation.
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")){
Instantiate(newObject, transform.position, Quaternion.Euler(transform.rotation.x+90, transform.rotation.y, transform.rotation.z));
}
}
And here's the part that REALLY confuses me. If I use this instead:
Instantiate(newObject, transform.position, transform.rotation);
//This code doesn't allow me to slightly adjust the angle of firing
This code will launch the the projectile down the camera's Z axis, like I expect. So why doesn't it work when I use Quaternion.Euler instead?
Answer by ScroodgeM · Aug 25, 2012 at 10:07 PM
use this line
Instantiate(newObject, transform.position, transform.rotation);
instead of
Instantiate(newObject, transform.position, Quaternion.Euler(transform.rotation.x+90, transform.rotation.y, transform.rotation.z));
transform.rotation is Quaternion. don't use it's components unless you understand how it works.
addon: if you want to rotate from initial rotation, use
C#:
Quaternion offsetFromForward = Quaternion.Euler(10f, 10f, 0f); Instantiate(newObject, transform.position, transform.rotation * offsetFromForward);
JS:
var offsetFromForward : Quaternion = Quaternion.Euler(10f, 10f, 0f); Instantiate(newObject, transform.position, transform.rotation * offsetFromForward);
Thank you for responding, but that's a non-answer. :(
EDIT: Never$$anonymous$$d, you updated.
first line was an answer 8) check it again, i'd explain it a little 8)
Your suggestion is close to what I need, but there are two problems:
"transform.up" sounds like a simple 90 degree rotation. What if I want 80 degrees? What if I want to fire a projectile with some random variance in accuracy? What is the command to input each x,y,z direction individually? That's what I need.
I get this error when I run your code: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Vector3, UnityEngine.Vector3)' was found
i'd explain answer about it. i saw "x+90" and had think that you need up/down 8)
my mistake, fixed.
check answer again.
So sorry, but I get this error with your code
';' expected. Insert a semicolon at the end
For this line:
Quaternion offsetFromForward = Quaternion.Euler(10f, 10f, 0f);
Which doesn't make any sense to me because there's clearly a semicolon there.
Your answer
Follow this Question
Related Questions
instantiate gameobjects into area 1 Answer
Error when trying to instantiate 1 Answer
How to create GameObject (from prefab) at location of BoxCollider2D with rotation? 0 Answers
Door open animation by script 2 Answers
Script gives wrong rotation. 1 Answer