So I got this error that I couldn’t fix Wasted a week or so googling and haven’t found anything useful to my problem. I am following this tutorial
. The code I wrote is absolutely correct, I checked way too many times. It has something to do with unity. Please, help
eses
January 18, 2020, 10:39am
2
@justcallmepatrick69
“So I got this error that I couldn’t fix”
What error - care to share it here? Or is it the “transform.position assign attempt for “owl” is not valid”. Either way, context is unclear.
“It has something to do with unity.”
I somehow think you just have some error. If author of this tutorial got it working, you should too -
“The code I wrote is absolutely correct”
But it doesn’t work? I think you should post your code…
2 Likes
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hard : MonoBehaviour
{
bool moveAllowed;
Collider2D col;
void Start()
{
col = GetComponent<Collider2D>();
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
moveAllowed = true;
}
}
if (touch.phase == TouchPhase.Moved)
{
if (moveAllowed)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y);
}
}
if (touch.phase == TouchPhase.Ended)
{
moveAllowed = false;
}
}
}
}
second script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RandomPatrol : MonoBehaviour
{
public float minX;
public float maxX;
public float minY;
public float maxY;
Vector2 targetPosition;
public float minSpeed;
public float maxSpeed;
float speed;
public float secondsToMaxDifficulty;
public GameObject restartPanel;
// Start is called before the first frame update
void Start()
{
targetPosition = GetRandomPosition();
}
// Update is called once per frame
void Update()
{
if ((Vector2)transform.position != targetPosition)
{
speed = Mathf.Lerp(minSpeed, maxSpeed, GetDifficultyPercent());
transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
} else {
targetPosition = GetRandomPosition();
}
}
Vector2 GetRandomPosition() {
float randomX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
return new Vector2(randomX, randomY);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Alien")
{
restartPanel.SetActive(true);
}
}
float GetDifficultyPercent() {
return Mathf.Clamp01(Time.timeSinceLevelLoad / secondsToMaxDifficulty);
}
}
That’s the error I get when I press play
transform.position assign attempt for ‘Armature_Flying_2 (5)’ is not valid. Input position is { NaN, NaN, 0.000000 }.
UnityEngine.Transform:set_position(Vector3)
RandomPatrol:Update() (at Assets/RandomPatrol.cs:37)
This is your answer that you already had in the error message itself. You’re assigning a Vector3 where the X & Y are NaN (not a number). These can be created for various reasons but most llikely as a result of a math operation doing a divide by zero; something you can easily find more information about with a quick search.