project_01
project_01

Reputation: 31

How to change the coordinates of an object in unity 3D

Recently,I was learning unity 3d, I want to make an object move a distance by C#,I don't know if I'm right,that's what I write:

using UnityEngine;
using System.Collections;

public class sceneTransform : MonoBehaviour {

    public float speed=0.1f;
    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        if(transform.position.z<7){
            transform.position += new Vector3 (0.2,0,0)*speed*Time.deltaTime;
        }
    }
}

Upvotes: 0

Views: 2135

Answers (3)

Jonathan Van Dam
Jonathan Van Dam

Reputation: 648

I always just use transform.Translate() usually moves the object for me.

Upvotes: 0

Trafalgar
Trafalgar

Reputation: 145

The arguments for Vector3 need to be in float.

transform.position += new Vector3 (0.2f,0,0) * speed * Time.deltatime;

Upvotes: 3

It&#233;ration 122442
It&#233;ration 122442

Reputation: 2962

Concerning the error:

Assets/Scripts/sceneTransform.cs(15,68): error CS1502: The best overloaded method match for "UnityEngine.Vector3.Vector3(float, float, float)" has some invalid arguments,and Assets/Scripts/sceneTransform.cs(15,68): error CS1503: Argument "#1 " cannot convert "double" expression to type "float"

When working with floats in Unity3D, you need to put an "f" after them, like:

Vector3(0.2f,0f,0f);

Upvotes: 0

Related Questions