Why do I need to set the _offset.y = 0

Updated on February 11, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on February 10, 2019

hi, I’m new to Unity and want to learn about this concept before I move on. In order to get my code below to work, I needed to set the _offset.y = 0 and I needed to normalize this figure, can anyone explain to me why you would set the _offset.y to 0. I get why you Normalize it but not why you would set the Y value to zero  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Follow : MonoBehaviour {
[SerializeField]
Transform target;

public float smoothTime;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update()
{

Vector2 _offset = target.position – transform.position;
_offset.y = 0.0f;
_offset.Normalize();
transform.position = new Vector3(target.position.x – _offset.x, transform.position.y, -10);

}

}

  • Liked by
Reply
1 on February 11, 2019

Who told you that you needed to set Y to 0?

I’m not sure what the final intentions with this script are, but not only do you not need to set Y = 0, you don’t need to normalize it either.

Normalizing sets the length to 1. _offset is a 2D vector. If you set the Y to 0, then all that’s left is X. Normalizing that just’s sets X = 1. (1, 0)

 

Is this for a 2D or 3D game, and did you get this code from a 2D or 3D game? Because if this was a 3D vector, and you set the Y = 0, then that would give you the direction something is to something-else, ignoring elevation.

on February 11, 2019

Thanks for answering the question, I saw it in a video but also it doesn’t actually work without doing those two things. Whether or not you meant to thanks for answering my question

Show more replies
  • Liked by
Reply
Cancel