Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by tsdavs · Dec 12, 2015 at 08:02 AM · transformlerpsmoothtranslate3rd person camera

Smooth movement for a 3rd person camera

Hello,

So I have a 3rd person camera that I have made to switch from right to left and vice versa when you push "x".

I want to make switching from either side to the opposite side smooth and more natural feeling. It has to happen on the local axis as well. I've tried to Lerp, smoothdamp, and move towards but I'm relatively new and I've been having trouble. Help would be much appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class leftRightTPCameraSwitch : MonoBehaviour
 {
     public bool cameraRight = true;
     public bool cameraLeft = false;
     private Vector3 positionRight, positionLeft;
 
     void Awake()
     {
         positionRight = new Vector3(2, 0, 0);
         positionLeft = new Vector3(-2, 0, 0);
     }
 
     public void Update()
     {
         if (Input.GetKeyDown("x"))
         {
             if (cameraRight)  // Switch it to Left
             {
                 cameraRight = false;
                 transform.Translate(positionLeft); //move left if in right position               
                 cameraLeft = true;
             }
             else if (cameraLeft)   // Switch it to Right
             {
                 cameraLeft = false;
                 transform.Translate(positionRight); // move right if in left position
                 cameraRight = true;
             }
         }
     }
 }

Thanks for you help in advance!

Comment
Add comment
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

2 Replies

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

Answer by dhore · Dec 12, 2015 at 08:45 AM

I've made a few changes to your code which will hopefully give you your expected result. I'll explain it all at the bottom:

 using UnityEngine;
 using System.Collections;
 
 public class leftRightTPCameraSwitch : MonoBehaviour
 {
     public enum cameraState { right, left, movingRight, movingLeft };
     public cameraState state = cameraState.right;
     
     public float moveSpeed = 20;
 
     private Vector3 positionRight, positionLeft;
 
     void Awake()
     {
         positionRight = new Vector3(2, 0, 0);
         positionLeft = new Vector3(-2, 0, 0);
     }
 
     public void Update()
     {
         if (state == cameraState.movingRight)
         {
             transform.localPosition = Vector3.Lerp(transform.localPosition, positionRight, moveSpeed * Time.deltaTime);
             
             if (transform.localPosition == positionRight)
                 state = cameraState.right;
         }
         else if (state == cameraState.movingLeft)
         {
             transform.localPosition = Vector3.Lerp(transform.localPosition, positionLeft, moveSpeed * Time.deltaTime);
             
             if (transform.localPosition == positionLeft)
                 state = cameraState.left;
         }

         
         if (Input.GetKeyDown("x"))
         {
             if ((state == cameraState.right) || (state == cameraState.movingRight))  // Switch it to Left
             {
                 state = cameraState.movingLeft;
             }
             else if ((state == cameraState.left) || (state == cameraState.movingLeft))   // Switch it to Right
             {
                 state = cameraState.movingRight;
             }
         }
     }
 }

Okay, so firstly I've replaced your 2 boolean variables for right and left with an enum, this is for 2 reasons:

  1. It's bad practise to use multiple booleans to control the state of a system, as it leaves many potential flaws. For example, what if your cameraRight and cameraLeft were both equal to false? Your code wouldn't know what to do. Using an enum instead solves this potential problem.

  2. An enum can support as many states as you would ever need, which allows us to now know when it's moving and which direction it's moving in, as well as when it's stationary at one of the sides.

Using this new enum I re-arranged your Update function and made it use Vector3.Lerp for the movement (I also added a public float for the movement speed). So now it will Lerp over to the other position when it's told to. And it also won't allow input until it's reached the other side.

Comment
Add comment · Show 4 · 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 tsdavs · Dec 12, 2015 at 11:54 AM 0
Share

You legend! I was looking at this for hours and hours haha! I'll take you advice on about the booleans/enum in the future.

If you wouldn't $$anonymous$$d, what would I need to do if I wanted to input before it reached the other side? $$anonymous$$eaning, if I wanted to hotswap back to the right if I initiated a swap to the left but changed my $$anonymous$$d quickly and didn't want to wait for it to reach the other side.

Also hopefully I'm not taking too much time, how do I have a Vector to be a decimal? Reading:

 new Vector3(1, 1.5, -4) 

This is Just based on how it will sit. The camera is a child of the character sitting at y=0.5 so I'm looking for the camera to sit at y=2.

avatar image tsdavs tsdavs · Dec 12, 2015 at 12:31 PM 0
Share

All good with that decimal point! http://answers.unity3d.com/questions/173094/show-vector3-full-float-value.html Cheers!

avatar image dhore tsdavs · Dec 12, 2015 at 02:26 PM 1
Share

I made an edit to the last if statement in the Update function so you should now be able to switch directions even when it's already moving.

avatar image tsdavs dhore · Dec 13, 2015 at 12:19 AM 1
Share

Thanks buddy! I was sceptical about asking the community for help but you really raised the bar here! Super helpful and really quick!

avatar image
0

Answer by DarkSapra · Apr 11, 2019 at 07:55 AM

Wow, i think it's very complicated this one, too much lines (referencing to @dhore answer). You could change every enum with a simple bool. True f it's right, false if it's left, and simple switch between states.

  using UnityEngine;
  using System.Collections;
  
  public class leftRightTPCameraSwitch : MonoBehaviour
  {
      public bool Left;
      
      public float moveSpeed = 20;
  
      private Vector3 positionRight, positionLeft;
  
      void Awake()
      {
          positionRight = new Vector3(2, 0, 0);
          positionLeft = new Vector3(-2, 0, 0);
      }
  
      public void Update()
      {
          if (!Left)
          {
              transform.localPosition = Vector3.Lerp(transform.localPosition, positionRight, moveSpeed * Time.deltaTime);
             
          else if (Left)
          {
              transform.localPosition = Vector3.Lerp(transform.localPosition, positionLeft, moveSpeed * Time.deltaTime);
          }
          
          if (Input.GetKeyDown("x"))
          {
              Left = !Left
          }
      }
  }
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

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

35 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 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

Trying to move the Player 30 units on the x axis back and forth when clicking but transform.Translate not working reliably? 1 Answer

Move GameObject with transform.position 1 Answer

Vector3.Lerp not cooperating with me 0 Answers

Vector2.lerp dashing problem 0 Answers

How do I code transform translate in unity3d/ Not working right 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