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!
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:
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
andcameraLeft
were both equal to false? Your code wouldn't know what to do. Using an enum instead solves this potential problem.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.
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.
All good with that decimal point! http://answers.unity3d.com/questions/173094/show-vector3-full-float-value.html Cheers!
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.
Thanks buddy! I was sceptical about asking the community for help but you really raised the bar here! Super helpful and really quick!
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
}
}
}
Your answer
Follow this Question
Related Questions
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