- Home /
What is the best way to make smooth camera movement and rotation? (with my script)
Right now the way my camera moves is by being inside of the player and following the player movement.
Player Script
Vector3 moveHorizontal = MyCamera.transform.right * Input.GetAxisRaw("Horizontal") * speed;
Vector3 moveVertical = MyCamera.transform.forward * Input.GetAxisRaw("Vertical") * speed;
// rb getting the rigidbody Component
rb.velocity = (moveHorizontal + moveVertical);
Camera Script
public Vector3 myPos; //camera position
public Transform myPlay; // player position
void Update () {
transform.position = myPlay.position + myPos;
}
I also have another method of camera rotation when certain buttons are pressed.
Camera Script
private float goodDegs = 2.0f;
private float badDegs = -2.0f;
Void Update () {
if (Input.GetKey (KeyCode.E)) {
for (float i = 0; i < 90; i++) {
transform.Rotate (0, goodDegs * Time.deltaTime, 0);
}
}
if (Input.GetKey (KeyCode.Q)) {
for (float m = 0; m < 90; m++) {
transform.Rotate (0, badDegs * Time.deltaTime, 0);
}
}
}
What I want to do is turn it into a smooth rotation rather than just quickly jumping to new positions, I've looked elsewhere, but am not too sure what to use.
I am no expert, most of what I've done has been with help from forum answers.
Answer by YoungDeveloper · Jul 28, 2015 at 04:23 AM
Instead of applying hard rotation, use slerp or lerp to smooth it out. Difference between them is perfectly showcased in this video, where lerp is the linear line.
I'm not too sure how to use slerp and lerp. I tried setting up something with another forum's answer that looked something like this...
private bool moving = true;
Void Update () {
if (moving) {
Vector3 moveHorizontal = $$anonymous$$yCamera.transform.right * Input.GetAxisRaw("Horizontal") * speed;
Vector3 moveVertical = $$anonymous$$yCamera.transform.forward * Input.GetAxisRaw("Vertical") * speed;
rb.velocity = (moveHorizontal + moveVertical);
}
if (!moving) { // Not too sure what to fill in
StartCoroutine($$anonymous$$oveObject(transform, 0, 1, 0f, 1f, 0.5f));
}
}
IEnumerator $$anonymous$$oveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, Quaternion startRot, Quaternion endRot, float time) {
moving = true;
float i = 0;
float rate = 1 / time;
while (i < 1) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp (startPos, endPos, i);
thisTransform.rotation = Quaternion.Slerp (startRot, endRot, i);
yield return 0;
}
moving = false;
}
...but I didn't know what to fill in inside the brackets (ThisTransform, Vector3 startPos, ...). I don't even know if this would work since I never got it working properly.
Your answer
Follow this Question
Related Questions
Smooth Camera Movement 1 Answer
Smooth Camera/Object Movement 1 Answer
Move along grid where facing 1 Answer
How to get smooth camera movement with smooth follow? 0 Answers