- Home /
Script won't set right rotation?
So i have this script where I want to set the localrotation and the localposition of my gun when picked up. The localposition works but setting localrotation won't set it do what I want it too unless it's 0.
using UnityEngine;
using System.Collections;
public class Positioning : MonoBehaviour {
public Vector3 localPosition;
public Quaternion localRotation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PositionGun(){
transform.localRotation = Quaternion.identity;
transform.localPosition = localPosition;
transform.localRotation = localRotation;
}
}
Local position and rotation take the parent transform tree into account. Try setting transform.rotation ins$$anonymous$$d to see if that helps? Honestly, I never use localPosition or localRotation. :)
That's what I'm trying to do, because I have to take the parents rotation into account as this gun will be relate to the player and not the world.
You sure you want to use a quaternion and not a vector?
public Vector3 localRotation;
public void PositionGun(){
transform.localRotation = Quaternion.identity;
transform.localPosition = localPosition;
transform.localEulerAngles = localRotation;
}
Answer by aldonaletto · Feb 12, 2013 at 03:58 AM
Usually that's not the way to do the job: create an empty object, child it to the player and adjust its position and rotation. When you want to add the weapon, child it to the empty object and reset its local position/rotation: it will be exactly at the same position/rotation as the empty object, and both will follow the player wherever it goes:
using UnityEngine;
using System.Collections;
public class Positioning : MonoBehaviour {
public Transform weaponPlace; // drag the empty object here
public void PositionGun(){
transform.parent = weaponPlace; // child the gun to the empty object
transform.localRotation = Quaternion.identity; // reset local rotation...
transform.localPosition = Vector3.zero; // and local position
}
}
I'll probably have to fix some rotation in blender, but this fixes the problem. thanks.
If the weapon model has swapped axis, compensate this in the localRotation assignment (line 10). If the weapon model forward direction is Y, for instance, set localRotation to the appropriate rotation ins$$anonymous$$d of the neutral Quaternion.identity:
...
transform.localRotation = Quaternion.Euler(-90,0,0);
...
This way you can place the weapon script in the empty game object, since its local Z direction is the model forward direction - LookAt and LookRotation can be used to aim the weapon at the target.
Ah Thankyou!, I've spent the last few hours trying to snap my camera back to forward (For the $$anonymous$$ch) when i start moving and as i am using Quaternion.LocalRotation for the angles i was stuck on getting it back to however forward works with Quaternions... INDENTITY was the answer!
if (m_SnapBack)
{
m_CameraTargetRot = Quaternion.Slerp(m_CameraTargetRot, Quaternion.identity, m_Smooth * Time.deltaTime);
}
So $$anonymous$$udos to you Aldonaletto!