Hi there,
I am trying to move my camera with an object(only sometimes, please do not tell me to parent the object to it). But it moves towards the object instead of the same direction and afterwards it goes in a circle. I can’t understand why it does this as I am using only one direction on one axis for now.
The script:
[source]
using System.Collections.Generic;
using UnityEngine;
public class lore : MonoBehaviour {
public Rigidbody player;
public float movespeed;
public float movespeed_corner;
public int nextdirection;
private int direction,lastdirection;
public const int up=1;
public const int down=4;
public const int left=8;
public const int right=2;
public const int stop=0;
public bool withcamera;
private GameObject camera;
// Use this for initialization
void Start () {
withcamera=true;
player=GetComponent();
direction=down;
camera=GameObject.Find(“Camera”);
}
// Update is called once per frame
void Update () {
nextdirection=direction;
if(lastdirection!=direction){
direction=0;
//player.rotation =new V
}
switch(direction)
{
case down:
player.velocity = new Vector3(0, 0,movespeed);
if(withcamera) {
camera.transform.Translate(0,0,movespeed);
}
break;
case up:
player.velocity = new Vector3(0, 0,-movespeed);
//if(wichcamera) {
// camera.transform.Translate(0,0,-movespeed);
//}
break;
case left:
player.velocity = new Vector3(0, movespeed,0);
//if(withcamera) {
// camera.transform.Translate(0,movespeed,0);
//}
break;
case right:
player.velocity = new Vector3(0, -movespeed,0);
//if(withcamera) {
// camera.transform.Translate(0,-movespeed,0);
//}
break;
default:
player.velocity = new Vector3(0, 0,0);
//if(withcamera) {
// camera.transform.Translate(0,0,0);
//}
break;
}
//withcamera=false;
direction=nextdirection;
lastdirection=direction;
}
}
[/source]
Can anybody tell me what I am doing wrong?