So I’ve been working on making a game for about a month or two now and in the game there is a cow (see image below) and I wrote some code for it to go to a specified location:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawn : MonoBehaviour
{
public Vector2 cowStart;
// Start is called before the first frame update
void Start()
{
transform.position = cowStart;
}
}
In the inspector in unity, I have ‘cowStart’ set to (0, 0) but I have also tried setting it to things like (1,0) and changing the numbers. There is only one other script applied to the cow and it just makes it move. I’m fairly certain it’s not doing anything to affect the position in a way that would sent it to the same location every time I start the game (I looked through it several times) but just in case, here is the code: (It’s really long so feel free to skip it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CowMove : MonoBehaviour
{
//variables for speed, direction, etc.
public float direction; //1-8 (See diagram below)
//vectors for where to lerp to.
Vector2 pos1;
Vector2 pos2;
Vector2 pos3;
Vector2 pos4;
Vector2 pos5;
Vector2 pos6;
Vector2 pos7;
Vector2 pos8;
public float speed;
public float timeX;
//Bools to control animation
public bool speedCheck;
//SPEED DIAGRAM
// *--[0]---[1]---[2]--*
//DIRECTION DIAGRAM
// <^ ^ ^>
// \ | /
// 1 2 3
// < - 8 4 - >
// 7 6 5
// / | \
// <v v v>
//NOTE: Diagonals are a difference of ~0.7 on the X and Y axis, whereas the vert and horiz ones are exactly 1.
//get the renderer to flip the sprite when it's moving to the right or left
public SpriteRenderer spRenderer;
void Start()
{
spRenderer = GetComponent<SpriteRenderer>();
float side = Random.Range(1, 3);
if (side == 1)
{
spRenderer.flipX = !spRenderer.flipX;
}
else if (side == 2)
{
spRenderer.flipX = !spRenderer.flipX;
}
}
void Update()
{
direction = Random.Range(1, 9);
//activate different lerp voids depending on the direction value
if (direction == 1)
{
//left up
dir1();
}
else if (direction == 2)
{
//up
dir2();
}
else if (direction == 3)
{
//up right
dir3();
}
else if (direction == 4)
{
//right
dir4();
}
else if (direction == 5)
{
//right down
dir5();
}
else if (direction == 6)
{
//down
dir6();
}
else if (direction == 7)
{
//down left
dir7();
}
else if (direction == 8)
{
//left
dir8();
}
//set each pos vector to the cow's position plus the default values.
pos1 = new Vector2(transform.position.x - 0.7f, transform.position.y + 0.7f) * speed;
pos2 = new Vector2(transform.position.x, transform.position.y + 1) * speed;
pos3 = new Vector2(transform.position.x + 0.7f, transform.position.y + 0.7f) * speed;
pos4 = new Vector2(transform.position.x + 1, transform.position.y) * speed;
pos5 = new Vector2(transform.position.x + 0.7f, transform.position.y - 0.7f) * speed;
pos6 = new Vector2(transform.position.x, transform.position.y - 1) * speed;
pos7 = new Vector2(transform.position.x - 0.7f, transform.position.y - 0.7f) * speed;
pos8 = new Vector2(transform.position.x - 1, transform.position.y) * speed;
}
void dir1() //left
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos1, Time.deltaTime * timeX);
}
void dir2()
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos2, Time.deltaTime * timeX);
}
void dir3() //right
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos3, Time.deltaTime * timeX);
}
void dir4() //right
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos4, Time.deltaTime * timeX);
}
void dir5() //right
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos5, Time.deltaTime * timeX);
}
void dir6()
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos6, Time.deltaTime * timeX);
}
void dir7() //left
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos7, Time.deltaTime * timeX);
}
void dir8() //left
{
//transform.position = Vector2.Lerp(transform.position, destination, Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, pos8, Time.deltaTime * timeX);
}
}
The problem is that every time I start the game, the cow goes to the exact same position: (-2.93496, -0.0168) and I have no clue why or how to fix this. Does anyone know how to get it to go to the right position? I eventually want to have 8-16 cows on the screen but when I do that, no matter how I arrange them, they go to the same spot. Here’s the picture for reference:
