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

2D movement help

Discussion in 'Scripting' started by mbschultz97, May 6, 2014.

  1. mbschultz97

    mbschultz97

    Joined:
    Feb 8, 2014
    Posts:
    11
    Hey guys, brand new to Unity and I'm trying to make a simple 2D platformer... So far I've been looking at different ways to make the movements and this is what I've come up with...

    using UnityEngine;
    using System.Collections;

    public class playercontrols : MonoBehaviour {
    public float characterSpeed = 10f;

    public void Update(){
    Transform.Translate (Input.GetAxis ("Horizontal") * characterSpeed * Time.deltaTime);
    }
    }


    The error that I get when I try to run my game is: Assets/playercontrols.cs(8,27): error CS0120: An object reference is required to access non-static member 'UnityEngine.Transform.Translate(UnityEngine.Vector3)'

    Thanks for the help :D
     
  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Lowercase transform is an instance of the Transform class that is an attribute of the MonoBehaviour.

    Uppercase Transform is the name of the class.
     
  3. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    Also I think you have to include Vector3.right (or what ever axis you want) in the timesing. That's at least how I do it.

    But yeah, above poster was right about capital Transform and lower case transform. Use lower case.
     
  4. mbschultz97

    mbschultz97

    Joined:
    Feb 8, 2014
    Posts:
    11
    So I changed Transform to transform and then I got two errors while trying to run the game... One saying it has invalid arguments and the other saying it can't convert a float to a vector3... I looked at a different script and put ,0,0 after Time.deltaTime and the game ran but I can't control the movement and also what does that ,0,0 do after Time.deltaTime?

    Thanks again! :D
     
  5. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    The Invalid arguments error is what pjde_ was addressing. Float is just a single number, but to move it wants X, Y and Z coordinates stored in what's called a Vector3.
     
  6. mbschultz97

    mbschultz97

    Joined:
    Feb 8, 2014
    Posts:
    11
    So to start making my character move do I have to use GetKeys and say transform.Translate(Input.GetAxis("Horizontal") * characterSpeed * Time.deltaTime,x = x+10,0 or something like that?
     
  7. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    new Vector3(x,y,z) creates a vector that takes float float float for xyz respectively. transform.Translate uses a Vector3 as an argument, like so: transform.Translate(new Vector3(x,y,z));
     
  8. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    And what pjde was getting at but I realize now wasn't as clear as I thought because he never mentioned multiplication,

    Code (csharp):
    1. transform.Translate (Input.GetAxis ("Horizontal") * characterSpeed * Time.deltaTime * Vector3.right);
     
  9. mbschultz97

    mbschultz97

    Joined:
    Feb 8, 2014
    Posts:
    11
    Because it's 2D I don't need the z coord, is there a Vector2 or do I just not put a third value in while using Vector3?