• 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 /
This question was closed Nov 30, 2013 at 08:15 AM by Julien-Lynge for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Samuel411 · Oct 05, 2013 at 01:44 PM · transformunity 4.2

Unity 4.2 Transform LocalPosition Not Working

Before the unity 4.2 update my code worked perfectly, now everything works except for moving the gun to the aim position. The field of view changes perfectly but not the transform.localPosition. Here's the code,

 var HitPose : Vector3;
 var AimPose : Vector3;
 
 var SprintRotation : Quaternion;
 var SprintPos : Vector3;
 private var DefaultRotation : Quaternion;
 
 private var MainCam : GameObject;
 private var GunCam : GameObject;
 
 static var CanAim : boolean = true;
 static var IsAiming : boolean = false;
 static var IsSprinting : boolean = false;
 static var CanSprint : boolean = true;
 
 var AimingZoom = 60;
 var AimSpeed = 0.5;
 
 
 function Start () {
      transform.localPosition = HitPose;
      MainCam = GameObject.FindGameObjectWithTag("MainCamera");
      GunCam = GameObject.FindGameObjectWithTag("WeaponCamera");
      DefaultRotation = transform.localRotation;
 }
 
 function Update () {
    if(Gun.IsReloading){
       StopAiming();
    }
    
    if((CanAim)&& !IsSprinting){
      if(Input.GetButton("Aim")){
         if(!Gun.IsReloading){
         Aim();
         }
      }      
    }
      if(!Input.GetButton("Aim")){
         StopAiming();
    }
    if(Input.GetButton("Sprint")){
    if((CanSprint)&& !IsAiming){
       Sprint();
       }
    }else{
         StopSprinting();
    }
 }
 
 function Aim(){
         IsAiming = true;
         transform.localPosition = Vector3.Lerp(transform.localPosition,AimPose,AimSpeed*Time.deltaTime);
         MainCam.camera.fieldOfView = AimingZoom;
         GunCam.camera.fieldOfView = AimingZoom;
 }
 
 function StopAiming(){
         IsAiming = false;
         transform.localPosition = Vector3.Lerp(transform.localPosition,HitPose,AimSpeed*Time.deltaTime);
         MainCam.camera.fieldOfView = 60;
         GunCam.camera.fieldOfView = 60;
 }
 
 function Sprint(){
      CanAim = false;
      IsSprinting = true;
      Crosshair.IsTriggered = false;
      //Gun.CanReload = false;
      //Gun.CanFire = false;
      transform.localRotation = Quaternion.Lerp(transform.localRotation,SprintRotation,5*Time.deltaTime);
      transform.localPosition = SprintPos;
 }
 
 function StopSprinting(){
      transform.localRotation = Quaternion.Lerp(transform.localRotation,DefaultRotation,5*Time.deltaTime);
      CanAim = true;
      Crosshair.IsTriggered = true;
      IsSprinting = false;
      //Gun.CanReload = true;
      //Gun.CanFire = true;
 }
Comment
Add comment · Show 9
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 whydoidoit · Oct 05, 2013 at 01:51 PM 0
Share

Well localPosition isn't broken I promise :) Are the HitPose and AimPose still set correctly?

avatar image Samuel411 · Oct 05, 2013 at 01:58 PM 0
Share

Yeah, I was trying to fix this issue all night nothing worked...

avatar image whydoidoit · Oct 05, 2013 at 02:00 PM 0
Share

I sure can't see anything wrong here - another script affecting the position perhaps?

avatar image Samuel411 · Oct 05, 2013 at 02:03 PM 0
Share

The only other thing would be the playhandler that turns this off if the network view isn't yours

avatar image whydoidoit · Oct 05, 2013 at 02:11 PM 1
Share

In Aim I'd debug the local position, the AimSpeed and the AimPose and see if it is changing - you'd expect it to be bouncing around a little if something else is messing with it, you'd expect it to be moving smoothly toward the AimPose otherwise.

avatar image Samuel411 · Oct 05, 2013 at 02:27 PM 0
Share

Wow biggest facepalm ever, a script was setting the aim speed to 0! Thank you so much!

avatar image whydoidoit · Oct 05, 2013 at 02:34 PM 0
Share

It's always something weird :) Code looks nice an clean - had to be the data or some dodgy other script :)

avatar image GreenDinoBV · Nov 29, 2013 at 01:55 PM 0
Share

Here too. The local position in unity inspector is completely different as set up from code.

avatar image giantkilleroverunity3d · Jan 18, 2019 at 11:15 PM 0
Share

$$anonymous$$ine too.

 float tempYPos  = 5393;
 transform.localPosition = new Vector3(0.0f, tempYPos, 0.0f);
 debug.log shows  transform.localPosition.y = 5393
 transform.position = new Vector3(0.0f, tempYPos, 0.0f);
 debug.log shows  transform.position.y = 5393

GameObject not positioned there and inspector shows Transform Position.y = 0

So where do I look to see where 5393 is co$$anonymous$$g from? The only thing I can think of is that the script is not looking at the gameobject that it is attached to?

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

using MoveObject to perform 2 translations at the same time 1 Answer

How to transform points from world to camera space and back again. 1 Answer

Child Transform 1 Answer

How to check if a variable has gone up or down 1 Answer

Children's position going haywire (Unity bug?) 1 Answer


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