The question is answered, right answer was accepted
I am trying to offset camera x distance from player all the time. Even if the player moves.
using UnityEngine;
public class MoveCam : MonoBehaviour {
public GameObject Sphere;
public GameObject Camera;
public float xOffset = 1;
public float yOffset = 1;
public float zOffset = 1;
void Start () {
}
void Update () {
this.transform.position = new Vector3(Sphere.transform.position.x + xOffset, Sphere.transform.position.y + yOffset, Sphere.transform.position.z + zOffset);
}
}
Thank you!
Answer by Matt1000 · Mar 16, 2017 at 08:56 PM
Actually that code works perfectly... perhaphs you are not setting the Sphere variable in the inspector, or you dont have the script attached to your camera.
Something else, your camera variable is useless in this case, since you are using this.transform.position
. Otherwise use Camera.transform.position
though I'd recommend delete that variable since its a name used a lot by Unity and may cause errors.
You should try to avoid names such as: Object, Camera, Transform, etc.
@$$anonymous$$att1000 thank you for ur help. I didn't set it to the game object in the inspector.
@Icey_Code could you pleasse select the answer as correct and up-vote please?? i really need it.
Follow this Question
Related Questions
I need help working out a C# script with error CS0120 1 Answer
InvalidCastException: Cannot cast from source type to destination type. 1 Answer
How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers
How do I fix this code below!? | 2 Answers
Error in Inventory Script 1 Answer