- Home /
Slerp camera rotation moves on it's own at runtime?
I have a script to rotate the camera with an input axis, it works mostly. The big problem is that it starts moving on run time without input, and when I use the input, it eventually resists going further, and drifts back to centre. Trying to figure out how to stop this, so the input axis just rotates the camera normally without restriction, but does nothing when no input is used...? I am beginner to C#, but am trying my hardest to learn.
there is a rigid body attached to the camera, set to no gravity, no drags, and mass of 1.
the axis inputs are set to gravity and sensitivity 3, and dead at 0.005.
the camera has one child, which is a light.
Any input is appreciated! Danke! ; D
using UnityEngine;
using System.Collections;
public class control_rotator : MonoBehaviour {
//rotate variables
public float horizontalSpeed = 0.3F;
public float verticalSpeed = 0.3F;
public float speed = 0.3f;
public Transform target; // set to the main camera (itself)
//movement variables
public float moveSpeed = 3.0f;
void Update() {
// input and transform rotation values
float h = horizontalSpeed * Input.GetAxis("Horizontal2");
float v = verticalSpeed * Input.GetAxis("Vertical2");
transform.Rotate(-v, -h, 0);
//locate and slerp rotation values
Vector3 relativePos = target.position - transform.position;
Quaternion where = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation, where, Time.deltaTime * speed);
// input and transform movement values
float moveHorizontal = moveSpeed * Input.GetAxis ("Horizontal");
float moveVertical = moveSpeed * Input.GetAxis ("Vertical");
//movement vectors and physics
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * moveSpeed;
}
}
Answer by robertbu · May 06, 2014 at 07:45 AM
Lines 29 - 34 are causing the object to try and look at the 'target'. Line 26 is trying to rotate based on user input. So you have two different kinds of rotation code fighting with each other. I'm not sure of your game needs, but deleting lines 29 - 34 will allow the user input to rotate the object without restriction.
thanks, that works! it also works with the target set to nothing. i think i got confused how slerp works, and it seems to still interpolate somehow?
Your answer
Follow this Question
Related Questions
Transition current camera rotation to 0,0,0 1 Answer
Rotation always changing values! 1 Answer
Rotate Camera in Increments with Damping 2 Answers
Gameobject Rotation Calculation 0 Answers