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);
}
}
}
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.
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.
//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.
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.
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.