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 player movemint

Discussion in 'Scripting' started by mmomaker, Jan 30, 2015.

  1. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    Hi guys my name is cuddle bunny I'm working on a project and I'm making a prototype But the 2d move mint isn't working I want my 2d charter to go in all 8 directions But my script isn't working

    1. usingUnityEngine;
    2. usingSystem.Collections;
    3. publicclassPlayerMovement:MonoBehaviour{
    4. publicfloat speed =1f;
    5. // Use this for initialization
    6. voidStart(){
    7. }
    8. // Update is called once per frame
    9. voidUpdate(){
    10. if(Input.GetKey(KeyCode.D)
    11. Transform.position +=newVector3(speed *Time.deltaTime,0.0f,0.0f);
    12. if(Input.GetKey(KeyCode.A)
    13. Transform.position -=newVector3(speed *Time.deltaTime,0.0f,0.0f);
    14. if(Input.GetKey(KeyCode.W)
    15. Transform.position +=newVector3(0.0f, speed *Time.deltaTime,0.0f);
    16. if(Input.GetKey(KeyCode.s)
    17. Transform.position -=newVector3(0.0f, speed *Time.deltaTime,0.0f);
    18. }
    19. }
    the error it giveing me is

    1. Assets/scrps/PlayerMovement.cs(15,29): error CS1525:Unexpected symbol `Transform'
    2. Assets/scrps/PlayerMovement.cs(18,29): error CS1525: Unexpected symbol `Transform'
    3. Assets/scrps/PlayerMovement.cs(21,29): error CS1525: Unexpected symbol `Transform'
    4. Assets/scrps/PlayerMovement.cs(24,29): error CS1525:Unexpected symbol `Transform'
    if you guys can tall me how to fix this i will be exstremly happy
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671

    Try using "transform" < Lower case t. Also, you do not need Vector3 as it is 2D movement, use Vector2.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Move Mint!

    <3<3<3<3<3
     
    Zalosath likes this.
  4. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    the lowe case t and vector2
    dint fix it
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Just keep Vector3, still use lower case t though.
     
  6. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    ok i tyed that it sill giveing the errors it funny my mono say evry thing ok but unity say ther a problum
     
  7. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Is the Script on the Game Character?
     
  8. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    yup why?
     
  9. IntegraC

    IntegraC

    Joined:
    Jan 20, 2015
    Posts:
    16
    Spaces are your friends. Not sure if its because you pasted it right into the post instead of using Insert>Code.

    But also, you needed another ) at the end of each if statement. You had if(Input.GetKey(KeyCode.D) it needed to be if(Input.GetKey(KeyCode.D))

    Anyway, here's the code you want:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement :MonoBehaviour
    5. {
    6.     public float speed = 1f;
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if(Input.GetKey(KeyCode.D))
    16.             transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);
    17.  
    18.         if(Input.GetKey(KeyCode.A))
    19.             transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);
    20.  
    21.         if(Input.GetKey(KeyCode.W))
    22.             transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);
    23.  
    24.         if(Input.GetKey(KeyCode.S))
    25.             transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
    26.     }
    27. }
    Play with the speed variable in the inspector if you want it to go faster.