Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

move cube

Discussion in 'Scripting' started by petea, May 16, 2014.

  1. petea

    petea

    Joined:
    Nov 29, 2013
    Posts:
    55
    There code for move cube, no if cubes 2 or .... what work? Tell me example this basis code?
    Code (csharp):
    1. public class CubeMove : MonoBehaviour {
    2. public float moveSpeed = 3f;
    3. // Use this for initialization
    4. void Start () {
    5.  
    6. }
    7.  
    8. // Update is called once per frame
    9. void Update ()
    10. {
    11. //Moves Forward and back along z axis //Up/Down
    12. transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical")* moveSpeed);
    13. //Moves Left and right along x Axis //Left/Right
    14. transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")* moveSpeed);  
    15. }
    16. }
     
  2. GingerNinja14

    GingerNinja14

    Joined:
    Aug 14, 2013
    Posts:
    16
    You are multiplying the translate by Input.GetAxis, you need to declare the get axis in its own variable. For Example:

    float h = Input.GetAxis("horizontal");
    float v = Input.GetAxis("Vertical");

    transform.Position = new Vector3(h, v, 0) * moveSpeed * Time.deltaTime;
     
  3. petea

    petea

    Joined:
    Nov 29, 2013
    Posts:
    55
    So this code not work, I not undestund whot error?
     
  4. petea

    petea

    Joined:
    Nov 29, 2013
    Posts:
    55
    Sorry i me not view this post.
     
  5. petea

    petea

    Joined:
    Nov 29, 2013
    Posts:
    55
    no I me work in C#
     
  6. petea

    petea

    Joined:
    Nov 29, 2013
    Posts:
    55
    consol view, not error, but code no work.

    using UnityEngine;
    using System.Collections;

    public class cubeMove : MonoBehaviour
    {
    public float moveSpeed = 3f;
    // Use this for initialization
    void Start()
    { }
    // Update is called once per frame
    void Update()
    {
    //Moves Forward and back along z axis //Up/Down

    transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed);

    //Moves Left and right along x Axis //Left/Right

    transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);

    // float h = Input.GetAxis("horizontal");
    // float v = Input.GetAxis("Vertical");
    // transform.position = new Vector3(h, v, 0) * moveSpeed * Time.deltaTime;
    }
    }
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    er... no... petea's attempt was following the first example on the reference page:

    http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html


    @petea: I've copied the script you pasted in and it works, make sure you have added the script to the cube

    $cubemovescript.png
     
  8. kr4ft3r

    kr4ft3r

    Joined:
    Feb 18, 2014
    Posts:
    10
    Did you attach this code to the cube game object?
    Did you try increasing moveSpeed?