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
0
Question by Griffo · Aug 13, 2012 at 02:42 PM · vector3lerpvector3.lerp

Vector3.Lerp move weapon up to players face

Why when I move a child object (a weapon) using Vector3.Lerp does it not return to its original position when walking around with the below code? All I'm trying to do is move my weapon up close to the players face, I also have a script that zooms the camera in so it looks like you are zooming into the target.

Any help would be appreciated, thanks.

 #pragma strict
 
 private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
 
 function Awake(){
 
  globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
 }
  
 function Start () {
 
 }
 
 function Update () {
 
 if (Input.GetButtonDown("Zoom In")){
 
 StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(-0.25, 0.05, 0), 1));
  }
 
 if (Input.GetButtonDown("Zoom Out")){
 
 StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(0.25, -0.05, 0), 1));
  }
 }
 
 function MoveToPosition(newPosition : Vector3, time : float)
 {
     var elapsedTime : float = 0;
     var startingPos = globalVars.GetComponent(GlobalVars).currentWeapon.transform.position;
     while (elapsedTime < time)
     {
         globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
         elapsedTime += Time.deltaTime;
         yield;
     }
 }
Comment
Add comment · Show 6
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 BLarentis · Aug 13, 2012 at 03:40 PM 0
Share

The code seems right. Try to check if the Zoom Out function is being called, and if it is try to make the camera follow the player without being a child, just to make sure that the parent orientation isn't messing up with the positions.

avatar image Griffo · Aug 13, 2012 at 05:57 PM 0
Share

But if I just use Translate it goes and returns where expected every time while walking around, but I wanted it to animate to the new position not just appear ..

     #pragma strict
 
 private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
 
 function Awake(){
 
  globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
 }
  
 function Start () {
 
 }
 
 function Update () {
 
 if (Input.GetButtonDown("Zoom In")){
 
 globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(-0.25,0.05,0));
  }
 
 if (Input.GetButtonDown("Zoom Out")){
 
 globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(0.25,-0.05,0));
  }
 }
avatar image BLarentis · Aug 13, 2012 at 06:04 PM 0
Share

Well, if your post up ahead is working, then now try to change the lines:

globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(-0.25,0.05,0));

globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(0.25,-0.05,0));

to this:

globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position, new Vector3(-0.25,0.05,0), 1 );

globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position, new Vector3(0.25,-0.05,0), 1 );

avatar image Griffo · Aug 13, 2012 at 06:11 PM 0
Share

BLarentis thanks for that but it just jumps to the position like my code above.

avatar image BLarentis · Aug 13, 2012 at 06:18 PM 0
Share

Try to change the time 1 to 0.2. Or in last case try to use ITween: http://itween.pixelplacement.com/index.php

Show more comments

2 Replies

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

Answer by Griffo · Aug 14, 2012 at 08:58 AM

If anyone else gets stuck with something like the problem I had here is the answer ..

     #pragma strict
 
 private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
 
 function Awake(){
 
  globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
 }
 
 function Start () {
 
 }
 
 function Update () {
 
 if (Input.GetButtonDown("Zoom In")){
  var zoomInOffset = Vector3(-0.25, 0.05, 0);
  var zoomInWorldPosition = transform.TransformDirection( zoomInOffset );
  StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomInWorldPosition, 0.5));
  }
 
 if (Input.GetButtonDown("Zoom Out")){
  var zoomOutOffset = Vector3(0.25, -0.05, 0);
  var zoomOutWorldPosition = transform.TransformDirection( zoomOutOffset );
  StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomOutWorldPosition, 0.5));
  }
 }
 
 function MoveToPosition(newPosition : Vector3, time : float){
     var elapsedTime : float = 0;
     var startingPos = globalVars.GetComponent(GlobalVars).currentWeapon.transform.position;
     
     while (elapsedTime < time){
         globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
         elapsedTime += Time.deltaTime;
         yield;
     }
 }
Comment
Add comment · 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
0

Answer by Dragonlance · Aug 13, 2012 at 06:07 PM

If the wapon is a child of the player, maybe you want to move the localPosition instead?

Comment
Add comment · Show 1 · 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 Griffo · Aug 13, 2012 at 07:30 PM 0
Share

Hi, localPosition don't work either ..

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

9 People are following this question.

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

Related Questions

How the heck does Mathf.Lerp work? 2 Answers

make my gameobject lerp between points more smoothly 0 Answers

vector3 lerp based on 2 different floats/axes? 1 Answer

Vector3.Lerp completing before t = 1? 2 Answers

Vector3.Lerp moves in the wrong direction 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