Home > Mobile >  I wanted to move the player on x position of the ground
I wanted to move the player on x position of the ground

Time:12-05

I'm currently making a 2D game like asteroids and I wanted the ground to follow the player X position but it give me an error this is the script:

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

public class playerFollow : MonoBehaviour
{

    public Transform playerPos;



    private void Update()
    {
        gameObject.transform.position = playerPos.position.x;
    }
}

This is the error message: cannot implicity convert type 'float' to 'UnityEngine.Vector3'

CodePudding user response:

The error says you are trying to assign a float (the x position of your palyerPos) to the position of your gameObject (which is a Vector3).

As you cannot just assign the x value of a transform.position I recommend you to buffer your gameObject position in a local variable, modify this variable and then reassign the modified position to the gameObject transform !

Hope I was clear enough :)

  • Related