FullMe7alJacke7
FullMe7alJacke7

Reputation: 21

Unity ViewtoWorldPoint not working, what did I do wrong?

I am trying to follow along this tutorial and make a Space Invaders replica, but I am probably going to tweak it later on to be more geometry wars style.

Any way I am teaching myself C# and Unity at the same time so it is rather difficult, I understand most of the code but some of it I do not, and that is where the hang up is I believe...

The code is supposed to move my formations Left and Right across the screen, but after hours of trying to edit my code and tweak it the only thing I have noticed is when I change the BoundaryLeftEdge and BoundaryRightEdge Vector 3's there is different behavior... but it still either moves to the left and moves back and fourth like it should except it is like offset from the screen or it moves to the right and gets stuck so on and so fourth... I want it to move back and fourth from point A to point B but I cannot seem to manage that :( Sorry I'm such a noob in need, Self teaching is more difficult than I figured I guess

using UnityEngine;
using System.Collections;

public class FormationController : MonoBehaviour
{   //Spawning Variables
    public GameObject EnemyPrefab;
    public float W = 10, H = 5;
    //Movement Variables
    public float Speed = 5;
    private int Direction;
    private float BoundaryRightEdge, BoundaryLeftEdge;
    public float Padding = 0.25f;


void Start() //Setting Boundary
{
    Camera camera = GameObject.Find("Player").GetComponentInChildren<Camera>();
    float distance = camera.transform.position.z - camera.transform.position.z;
    BoundaryLeftEdge = camera.ViewportToWorldPoint(new Vector3(0, 0, distance)).x + Padding;
    BoundaryRightEdge = camera.ViewportToWorldPoint(new Vector3(1, 1, distance)).x - Padding;
}

void OnDrawGizmos()
{
    float xmin, xmax, ymin, ymax;
    xmin = transform.position.x - 0.5f * W;
    xmax = transform.position.x + 0.5f * W;
    ymin = transform.position.y - 0.5f * H;
    ymax = transform.position.y + 0.5f * H;
    Gizmos.DrawLine(new Vector3(xmin, ymin, 0), new Vector3(xmin, ymax));
    Gizmos.DrawLine(new Vector3(xmin, ymax, 0), new Vector3(xmax, ymax));
    Gizmos.DrawLine(new Vector3(xmax, ymax, 0), new Vector3(xmax, ymin));
    Gizmos.DrawLine(new Vector3(xmax, ymin, 0), new Vector3(xmin, ymin));

}

void Update()
{
    float formationRightEdge = transform.position.x + 0.5f * W;
    float formationLeftEdge = transform.position.x - 0.5f * W;
    if (formationRightEdge > BoundaryRightEdge)
    {
        Direction = -1;
    }
    if (formationLeftEdge < BoundaryLeftEdge)
    {
        Direction = 1;
    }
    transform.position += new Vector3(Direction * Speed * Time.deltaTime, 0, 0);

    //Spawn Enemies on Keypress
    if (Input.GetKeyDown(KeyCode.S))
    {
        foreach (Transform child in transform)
        {
            GameObject enemy = Instantiate(EnemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
            enemy.transform.parent = child;
        }
    }
}

} `

Upvotes: 2

Views: 459

Answers (1)

Imtiaj Ahmed
Imtiaj Ahmed

Reputation: 350

According to your codes, I can only assume that formationRightEdge should be less than BoundaryLeftEdge and formationLeftEdge should be greater than BoundaryLeftEdge unless you are doing transformation in reverse direction in world point.

Try this -

if (formationRightEdge < BoundaryRightEdge)
{
    Direction = -1;
}
if (formationLeftEdge > BoundaryLeftEdge)
{
    Direction = 1;
}

Maybe your Direction = 1 and Direction = -1 should be reversed. And of course, check whether you set the values for your public variables in the inspector differently. You can reset to the default values there (defined in this script).

Upvotes: 1

Related Questions