Hi everyone, I’m having this problem with my player char. when moving. I have attached a camera to the Player [ a Cube ] and when I walk the camera shakes. I understand why this is happening, because it’s a cube and when I walk it doesn’t have smoothness or something… but I don’t know how to fix this, what should I do ? Here is my code attached to the player but I don’t think that there is the problem:
public class TransformFunctions : MonoBehaviour {
public float rotateSpeed;
public float speed;
public float timeLeft;
public int counter;
public float timercount;
private Light myLight;
private float lRange = 2.5f;
private float orange = 8f;
public AudioClip pick;
public AudioClip recharge;
public TextMesh Bateries;
public TextMesh timeLeft2die;
public TextMesh endText;
public TextMesh WinText;
// Called when the game starts.
void Start()
{
myLight = GetComponent<Light> ();
counter = 0;
timeLeft2die.text = "Time: "+timercount;
endText.text = "";
WinText.text = "";
}
// Update is called once per frame !!!!!
void Update ()
{
ColorChange ();
Movement ();
SetCountText ();
lightSubtract ();
TimeLeft ();
OnTriggerExit ();
ExitApp ();
}
/// collision detection
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "End") {
gameObject.GetComponent<TransformFunctions> ().enabled = false;
WinText.text = "You Win
press ESC to exit";
}
if (other.gameObject.tag == “PickUp”)
{
other.gameObject.SetActive(false);
audio.PlayOneShot(pick, 0.8f);
counter = counter + 1;
Bateries.text = "Bateries: "+counter;
}
}
//bateries count
void SetCountText()
{
if (Input.GetKeyDown (KeyCode.R) && counter > 0) {
counter = counter - 1;
myLight.light.intensity = light.intensity + 2.5f;
audio.PlayOneShot(recharge);
}
Bateries.text = "Bateries: " + counter;
while(counter < 0){
counter = 0;
}
}
void TimeLeft()
{
if (timercount > 0) {
timercount -= Time.deltaTime;
}
while (timercount < 0) {
timercount = 0;
}
timeLeft2die.text = "Time: " + timercount;
}
void OnTriggerExit (){
if (timercount == 0 && gameObject.tag == "Player") {
gameObject.GetComponent<TransformFunctions>().enabled = false;
endText.text = "GAME OVER";
}
}
//movement
void Movement(){
if (Input.GetKey (KeyCode.UpArrow)) {
transform.Translate (Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.Translate (Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Rotate (Vector3.down * rotateSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
}
if (Input.GetKeyDown (KeyCode.Space)) {
myLight.enabled = !myLight.enabled;
}
if (Input.GetKeyDown (KeyCode.LeftAlt)) {
myLight.type = LightType.Point;
myLight.range = lRange;
} else if (Input.GetKeyDown (KeyCode.LeftShift)) {
myLight.type = LightType.Spot;
myLight.range = orange;
}
}
void ExitApp(){
if (Input.GetKey (KeyCode.Escape)) {
Application.Quit ();
}
}
//flashlight color change
void ColorChange(){
if (Input.GetKey (KeyCode.Keypad0) || Input.GetKey (KeyCode.F1)) {
myLight.light.color = Color.cyan;
}
if (Input.GetKey (KeyCode.Keypad1)|| Input.GetKey (KeyCode.F2)) {
myLight.light.color = Color.red;
}
if (Input.GetKey (KeyCode.Keypad2)|| Input.GetKey (KeyCode.F3)) {
myLight.light.color = Color.yellow;
}
if (Input.GetKey (KeyCode.Keypad3)|| Input.GetKey (KeyCode.F4)) {
myLight.light.color = Color.green;
}
if (Input.GetKey (KeyCode.Keypad4)|| Input.GetKey (KeyCode.F5)) {
myLight.light.color = Color.magenta;
}
}
void lightSubtract ()
{
myLight.light.intensity -=Time.deltaTime * 0.1f;
}
}
How to remove the camera shake when walking with the Player [ cube ] ? Thanks!