Move the camera with an object or not

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?

It works with the line
vamera.transform.Translate(new Vector3(0,0,movespeed)*Time.deltaTime,Space.World);

A simple way to get a camera to follow by script is to find the offset:

Vector3 offset;
void Start(){
offset = transform.position - target.transform.position;
}

void Update(){

transform.position = target.transform.position + offset;
 
}

Might not work for you purposes, I don’t know.
Please check the first couple posts on using code tags.

Yes, please refer to this page for posting code on the forums! : Using code tags properly

It’s not your first post :slight_smile:

Hopefully that script helps you, even though it’s missing a few fun words like “void” (x2) :wink:

oops

:slight_smile:

Thank you

But this should not be in start but when the followstatus changes :wink:
Thank you

Well, it was an example, but the offset is a constant, so it isn’t supposed to be in Update if that’s what you mean. You would get the offset before the camera follow in any function.

That’s wrong. The offset is no constant if you don’t follow the whole time.

It’s a constant during the following process. It would only change if the camera moved when not following. Then it would have to be re-assessed.

Yes, when the status changes. Like I said.

It was just an example to show how to get the offset. I thought it would be understood from that amount of code.

Place you camera as a child of an empty gameobject (not the player) then move that gameobject where you want it & the camera will follow keeping any offsets & rotations.