I can't get my script to work!?

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

//Return direction align with Camera
Vector3 targetDirection
{
get
{
Vector3; cameraForward = mainCamera.TransformDirection(Vector3.forward);
cameraForward.y = 0; //set to 0 because of camera rotation on the X axis

//get the right-facing direction of the camera
Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);

//determine the direction the player will face based on input and the camera's right and forward directions
return Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
}
}

//This method needs to run on Update
void RotateWhileMoving()
{
if(speed > 0.5f)
{
//normalize the direction the player should face
Vector3 lookDirection = targetDirection.normalized;

//rotate the player to face the correct direction ONLY if there is any player input
if (lookDirection != Vector3.zero)
{
newRotation = Quaternion.LookRotation(lookDirection);

transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 4.5f * Time.deltaTime);
}
}
}

But I keep getting these errors


But the problem is whenever I add the semicolons, i get this error
How do I fix this?

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.

  • Vector3; cameraForward = mainCamera.TransformDirection(Vector3.forward);

  • Gives errors for me

  • Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);

  • No errors for me

You probably should test your script in an empty scene.

I was one of the answers :stuck_out_tongue:

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.

Vector3 pos = new Vector3(1,1,1);
var pos : Vector3;
pos = Vector3(1,1,1);

Logic is the same in most languages. The issue people have between languages is syntax.

Oh. I saw the keyword “get” so I thought it was in C#. My bad :wink:

But the error comes from this line :stuck_out_tongue:

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.

Sorry, I had the script pasted from earlier. The one I have now doesn’t have them

Plus it’s telling me to add them.

First things first, are you coding in javascript or C#? It sounds like even the compiler is confused, haha.

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

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.

//Return direction align with Camera
Vector3 targetDirection
{
get
{
Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);
cameraForward.y = 0; //set to 0 because of camera rotation on the X axis

//get the right-facing direction of the camera
Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);

//determine the direction the player will face based on input and the camera's right and forward directions
return Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
}
}

//This method needs to run on Update
void RotateWhileMoving()
{
if(speed > 0.5f)
{
//normalize the direction the player should face
Vector3 lookDirection = targetDirection.normalized;

//rotate the player to face the correct direction ONLY if there is any player input
if (lookDirection != Vector3.zero)
{
newRotation = Quaternion.LookRotation(lookDirection);

transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 4.5f * Time.deltaTime);
}
}
}

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.

It clearly looks like C#, but in that case a lot of stuff like the class declaration is missing.

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.

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.

1 Like

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

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.

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.

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.