• 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
Question by tayyab43 · Oct 26, 2013 at 09:30 AM · c#transformlerpaimdownsights

I want to lerp a gun from positionA to positionB in relation to the main camera

I'm making an aiming down the sight script and i'v made my gun a child of the main camera,i want to lerp it in relation to the main camera when the right mouse button is being held, and when its not,lerp it back to positionA.Heres what i'v got so far, BTW it's in c#:

using UnityEngine;

using System.Collections;

public class TheProperAimDownSight : MonoBehaviour{

bool lerp;

void Update (){

 if(Input.GetMouseButtonDown(0))
 {
     lerp = true;
 }
 if(Input.GetMouseButtonUp(0))
 {
     lerp = false;
 }
 
 if(lerp)
     transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime);
 else
     transform.position = Vector3.Lerp(positionB, positionA, Time.deltaTime);
 }

}

Comment

People who like this

0 Show 5
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 meat5000 ♦ · Oct 26, 2013 at 09:30 AM 0
Share

Instead of adding the gun to the camera, which complicates the coding, why not add a camera to the player?

This is your 7th question on this topic. Lots of the previous questions have been answered and accepted.

avatar image robertbu · Oct 26, 2013 at 03:20 PM 0
Share

I have not been involved in the other questions, but if you want to move 'in relation to the main camera', and the objects are children, then you want to use Transform.localPosition.

avatar image meat5000 ♦ · Oct 26, 2013 at 03:23 PM 0
Share

robert....

http://answers.unity3d.com/questions/561146/how-do-i-move-my-gun-to-position-b.html

You answered one of them :D

avatar image tayyab43 · Oct 26, 2013 at 07:28 PM 0
Share

I'm sorry for asking this many questions but I'm stuck, none of the answers really helped, also if i make the gun a child of the character, i can't look up down, left and right correctly.

avatar image tayyab43 · Nov 05, 2013 at 05:25 PM 0
Share

I'd really appreciate a solution

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Josh707 · Nov 05, 2013 at 06:29 PM

Instead of telling him/her how many times they have posted, why not just give a simple answer?

Your code is right, you just need to replace:

 transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime);

With:

 transform.localPosition = Vector3.Lerp(transform.localPosition, positionB, Time.deltaTime);

For both of the lerps. Using localPosition will have it move relative to it's parent (the camera) regardless of the parent rotating and moving, and if you want it to be able to smoothly go from any position inbetween to the target, it needs to lerp from it's current position. So if you let go before it is fully aimed, it won't snap to the aimed position and start moving back, it will be more like CoD per se.

Comment

People who like this

0 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 tayyab43 · Nov 08, 2013 at 04:04 PM 0
Share

you know what josh, you're my new favourite person, THANK YOU, ive been stuck on that for a while, and we all got to start somewhere right?

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.

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

17 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

Related Questions

I am trying to make a script that lerps when a button is pressed then lerps back but its not working? 3 Answers

How do I move my object slowly from its original position? 2 Answers

Distribute terrain in zones 3 Answers

Why does object lerp to above the object it is lerping to? 1 Answer

Unit rotation fails consistently on all slerp rotations after the first? 0 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