Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by chrisall76 · Feb 12, 2013 at 02:38 AM · transformpositioninglocalpositionlocalrotation

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;
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Feb 12, 2013 at 02:54 AM 1
Share

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. :)

avatar image chrisall76 · Feb 12, 2013 at 03:14 AM 0
Share

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.

avatar image PAEvenson · Feb 12, 2013 at 03:34 AM 0
Share

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;
     }

 

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chrisall76 · Feb 12, 2013 at 06:58 AM 0
Share

I'll probably have to fix some rotation in blender, but this fixes the problem. thanks.

avatar image aldonaletto · Feb 12, 2013 at 02:31 PM 0
Share

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.

avatar image ZenMicro · Sep 26, 2015 at 10:01 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.localPosition problem. 0 Answers

How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer

Issue with transform rotations and local rotations 1 Answer

probuilder position problem 2 Answers

Write a C# script to record GameObject rotation? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges