Hi! I am in the middle of translating the RigidbodyPhysicsFPS (found here) script from JavaScript to C#. and I have a couple of errors that I can not seem to figure out how to resolve. Any help would be greatly appreciated!
Here are the errors:
Assets/Standard Assets/Character Controllers/Sources/Scripts/RigidBodyPhysicsFPSCharacterController.cs(67,54): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Assets/Standard Assets/Character Controllers/Sources/Scripts/RigidBodyPhysicsFPSCharacterController.cs(72,36): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Assets/Standard Assets/Character Controllers/Sources/Scripts/RigidBodyPhysicsFPSCharacterController.cs(72,27): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)’ has some invalid arguments
Assets/Standard Assets/Character Controllers/Sources/Scripts/RigidBodyPhysicsFPSCharacterController.cs(72,27): error CS1503: Argument #1' cannot convert
object’ expression to type `UnityEngine.Vector3’
Line 67:
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
Line 72:
rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
The code:
using UnityEngine;
using System.Collections;
public class RigidBodyPhysicsFPSCharacterController : MonoBehaviour {
public float speed = 10F;
public float gravity = 10F;
public float maxVelocityChange = 10F;
public bool canJump = true;
public bool canFly = false;
public float jumpHeight = 2F;
bool grounded = false;
// @script RequireComponent(Rigidbody, CapsuleCollider)
void Awake ()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
void FixedUpdate ()
{
Vector3 velocityChange;
Vector3 velocity;
float v = Input.GetAxis("Analog Left Vertical");
float h = Input.GetAxis("Analog Left Horizontal");
float a = Input.GetAxis("Analog Right Vertical");
float r = Input.GetAxis("Analog Right Horizontal");
// Are we moving the Camera?
if (grounded)
{
// Calculate how fast we should be moving
Vector3 targetVelocity = new Vector3(h, 0F, v);
float targetAngle = 0F;
Quaternion targetRotation = Quaternion.identity;
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Rotate the Avatar to look at the direction we are wanting to move.
if (v > 0.2)
{
// Determine the angle we are rotating at, but only if it is not ZERO!
if (h > 0.1 || h < 0.1)
{
targetAngle = (90F * h) / 100F;
transform.Rotate(0F, targetAngle, 0F);
}
// transform.Rotate = targetRotation;
}
// Apply a force that attempts to reach our target velocity
velocity = rigidbody.velocity;
velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump Input.GetButton("Button Y"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay ()
{
grounded = true;
}
float CalculateJumpVerticalSpeed ()
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}