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

I can't get my script to work!?

Discussion in 'Scripting' started by Treasureman, Dec 27, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I recently got a script that is supposed to make the player rotate in the direction of the camera when he moves. Here's the script
    Code (JavaScript):
    1. //Return direction align with Camera
    2. Vector3 targetDirection
    3. {
    4. get
    5. {
    6. Vector3; cameraForward = mainCamera.TransformDirection(Vector3.forward);
    7. cameraForward.y = 0; //set to 0 because of camera rotation on the X axis
    8.  
    9. //get the right-facing direction of the camera
    10. Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);
    11.  
    12. //determine the direction the player will face based on input and the camera's right and forward directions
    13. return Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
    14. }
    15. }
    16.  
    17. //This method needs to run on Update
    18. void RotateWhileMoving()
    19. {
    20. if(speed > 0.5f)
    21. {
    22. //normalize the direction the player should face
    23. Vector3 lookDirection = targetDirection.normalized;
    24.  
    25. //rotate the player to face the correct direction ONLY if there is any player input
    26. if (lookDirection != Vector3.zero)
    27. {
    28. newRotation = Quaternion.LookRotation(lookDirection);
    29.  
    30. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 4.5f * Time.deltaTime);
    31. }
    32. }
    33. }
    But I keep getting these errors Screenshot (61).png
    But the problem is whenever I add the semicolons, i get this error Screenshot (62).png
    How do I fix this?
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I already saw your post somewhere and one the Unity's member answered to your question...

    At line 6, you have "Vector3;". Remove this semicolon.
     
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    1. Vector3; cameraForward = mainCamera.TransformDirection(Vector3.forward);
      - Gives errors for me

    2. Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);
      - No errors for me

      You probably should test your script in an empty scene.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I was one of the answers :p

    He's posting in javascript so if that's supposed to be javascript then he wants var varname : type, not type varname. That'll give errors even if it's done correctly because it's different in javascript.

    I'm a TA in a gamedev class and I teach in C# so I've already answered this question over a hundred times lol.

    Code (CSharp):
    1. Vector3 pos = new Vector3(1,1,1);
    Code (JavaScript):
    1. var pos : Vector3;
    2. pos = Vector3(1,1,1);
    Logic is the same in most languages. The issue people have between languages is syntax.
     
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Oh. I saw the keyword "get" so I thought it was in C#. My bad ;)

    But the error comes from this line :p
     
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    That's just the thing. "Vector3; cameraForward" looks like a confused mix of JS and CS. Maybe the semicolon was meant to be a colon, ala JS. But the order is backwards. The way it's set up is CS, with the incorrect addition of a delimiter between the type and identifier.

    It's hard to tell what it's supposed to be xD If it's CS, removing the semicolon will fix it. If it's JS, it's backwards and needs a colon so it will throw another error.
     
  7. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Sorry, I had the script pasted from earlier. The one I have now doesn't have them
     
  8. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Plus it's telling me to add them.
     
  9. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    First things first, are you coding in javascript or C#? It sounds like even the compiler is confused, haha.
     
  10. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I actually got the script from a guy on YouTube who made a prototype for his third person shooter game. Here's the video,



    He sent me the code in a private message but didn't specify whether it was JavaScript or C#. It shows the script I'm using at about 0:43 in the video
     
    Last edited: Dec 27, 2014
  11. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Can you give us the code which fails (if it doesn't the one in the first message) ?

    Sometimes, you shouldn't trust the compiler, he may says stupid things because of an error.
     
  12. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Code (JavaScript):
    1. //Return direction align with Camera
    2. Vector3 targetDirection
    3. {
    4. get
    5. {
    6. Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);
    7. cameraForward.y = 0; //set to 0 because of camera rotation on the X axis
    8.  
    9. //get the right-facing direction of the camera
    10. Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);
    11.  
    12. //determine the direction the player will face based on input and the camera's right and forward directions
    13. return Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
    14. }
    15. }
    16.  
    17. //This method needs to run on Update
    18. void RotateWhileMoving()
    19. {
    20. if(speed > 0.5f)
    21. {
    22. //normalize the direction the player should face
    23. Vector3 lookDirection = targetDirection.normalized;
    24.  
    25. //rotate the player to face the correct direction ONLY if there is any player input
    26. if (lookDirection != Vector3.zero)
    27. {
    28. newRotation = Quaternion.LookRotation(lookDirection);
    29.  
    30. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 4.5f * Time.deltaTime);
    31. }
    32. }
    33. }
    34.  
     
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    That's high school level CS, haha. One of the first things I learned was that the compiler frequently tells you there's an error on line x but it's being caused by a syntax problem on line y.

    @Treasureman that syntax looks like CS. Now that we know it's CS, what's the problem? Make sure your file is saved as .cs and not .js.
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It clearly looks like C#, but in that case a lot of stuff like the class declaration is missing.
     
  15. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I changed it to .cs and I got a parsing error on line (3,1). Sorry but I'm not great at coding, I haven't been able to go to any classes since there's none around me.
     
  16. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I can't say I know what's supposed to be going on in the first few lines of that script. Type identifier { get {

    I'm lost already. I see you have 2 closing braces so maybe that's supposed to be a function, in which case you're missing parenthesis. But you have get there, so maybe it's not a function. Someone who uses get and set regularly might have an easier time interpreting that.
     
    Treasureman likes this.
  17. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Sorry for the confusion. I sent it in to one of my friends that works for respawn entertainment who's been helping me. He'll try to fix it up a little
     
  18. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Is it the entire code ? Because the class name is missing.. In c# you need the class name otherwise the compiler will understand nothing (that's why he's crying at line 3)

    Can't help you for now, I'm going back home so I'll be able to help you more this evening.
     
  19. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    That's also why I suspected JS at first lol. When you see how JS files are laid out for unity, it's a real confusing mess if you're coming from CS.
     
  20. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    It's not telling you to add them after the data type, that's incorrect syntax. Just remove that one at Vector3 and it should work.
     
  21. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Do the other scripts are in JS or C# (check the extension file ; .js or .cs) ?

    Because if your entire project is in JS, you just can't change the extension file "like this". Also, you should read some tutorials because the errors you get are syntax errors and you could avoid that if you know the basics..
     
  22. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @Sbizz you can, it just isn't the greatest idea. Especially if you want to be able to access a script from another script. Mixing js and cs in a project is doable but bad.

    It looks to me like the tutorial was cs, so what he really needs I guess is a CS100 class.