x


object facing wrong way, Java script Quaternion

so i got an issue with a turret in my game, i want it to look at the person and shoot, obviously. Well its looking to the left (from its prospective, right from mine) when it should be looking at me. Here is the update function i have (LookAtTarget is a transform var set as the player)

function Update()
{
    if(LookAtTarget)
        {
            var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

            transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
            var seconds : int = Time.time;
            var oddeven = (seconds % 2);
            if(oddeven)
            {
                Shoot(seconds);
            }
        }

so yeah need a way to constantly make its rotation 90 degrees more to look at me, please help

more ▼

asked Apr 01 '10 at 01:50 AM

Adam Bruns gravatar image

Adam Bruns
274 24 27 38

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, the best way of fixing this would be to fix the orientation of your model's pivot, in your 3D app.

If that isn't possible, you can adjust the target quaternion by adding a line like this. I've changed your var name to "targetRotation" because it's a better description of what the variable contains.

function Update()
{
    if(LookAtTarget)
    {
        // calculate rotation towards target
        var targetRotation = Quaternion.LookRotation(LookAtTarget.position - transform.position);

        // adjust rotation by 90 degrees around y axis
        targetRotation *= Quaternion.Euler(0,90,0);

        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * damp);

        var seconds : int = Time.time;
        var oddeven = (seconds % 2);
        if(oddeven)
        {
            Shoot(seconds);
        }
    }
}

(and use -90 instead of 90 if it's rotated in the wrong direction!)

more ▼

answered Apr 01 '10 at 09:52 AM

duck gravatar image

duck ♦♦
41k 92 148 415

thanks, i went in and tried to change the rotation in blender, but for some reason it just changed the rotation in unity too, not the actual models rotation, so i ended up rebuilding the turret to face the right direction in blender (from front view straight up is forward...) because i was tired of waiting :P still thank you and i'll remember this for later :D

Apr 01 '10 at 06:59 PM Adam Bruns
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5071
x2163
x439
x374
x108

asked: Apr 01 '10 at 01:50 AM

Seen: 2836 times

Last Updated: Apr 01 '10 at 01:50 AM