Prefab acting differently in two scenes. I'm losing my mind

I’ve uploaded a video to demonstrate what happens. (Jump to 00:47)

https://www.youtube.com/watch?v=hZIYI6yhjZ4&feature=youtu.be

I want the rock to move right, in a straight line. Then fall down if there is a hole in the surface. Not do the confusing jumpy thing.

And btw. the cubes which the rock is standing on, are also of the same “ground” prefab in both levels.

Any help will be appreciated :slight_smile:

And here is some of the code I am using for my “Rock”.

    private PlayerController player;
	private bool dropped = false;
	private float originalY;
	private Vector2 newPos;
	private float speed = 5f;

void OnCollisionEnter2D(Collision2D coll) 
	{
		if (coll.gameObject.tag == "Player")
		{
				if (player.shape == 4)
				{
					this.newPos.x++;
					StartCoroutine(MoveRock(this.newPos, this.speed));
				}
	    }	
	}


void Start () {
		this.originalY = this.transform.position.y;
		this.newPos.x = this.transform.position.x;
		player = GameObject.Find("Stickman").GetComponent<PlayerController>();
	}

IEnumerator MoveRock(Vector2 newPos, float speed)
	{
		float timer = 0f;
		while (timer < 0.5f) //0.5
		{
			timer += Time.deltaTime;
			yield return null;

			float step = speed * Time.deltaTime;
			if (dropped == false)
			{
				this.transform.position = Vector2.MoveTowards(this.transform.position, newPos, step);
			}
			if (transform.position.y < originalY - 0.1f)
			{
				dropped = true;
			}
		}
	}

If you also think this is some kind of dark magic, please upvote this question.

3 Answers

3

Hello GimLee.

Try angularDrag 0.00

Or try to position player more far away of the box.

Regards.

Edit: Check ground properties maybe it has something to bounce.

Had no effect :(

Check my edit versión maybe is that. Hope it is.

No, because the ground is also the same in the 2 scenes.

Hey There,

The thing I could see is that you modified a thing that was actually your prefab in one scene and saved it like this but did not apply it to the prefab. So now in one scene you still have the “original” prefab and on the other you have modified it so to check it out, I would suggest to try instantiating the prefab via code. With this, you will be 100% sure that the object loaded are really the prefab that you have.

Hope it helps,

Claude

I have applied to the prefab a hundred times, this is not the problem. Thanks though

Now I also tried instantiating the prefab from code. Still no effect :(

The problem is here:

this.transform.position = Vector2.MoveTowards(this.transform.position, newPos, step);

In your script, you set newPos.x and then pass it into this function. You then tell the object to moveTowards newPos. You are updating the x, but not the y value of nowPos. The box is at -1.7 on the y-axis in your video. When you call MoveRock(), your rock is trying to move to 0.0 on the y-axis. Fix your problem by moving your rock to 0.0 on the y-axis OR change the line to this:

this.transform.position.x = Mathf.MoveTowards(this.transform.position.x, newPos.x, step);