i just tried adding a rigid body to the player and it doesnt seem to do anything, however when gravity is turned on, the player falls throught the floor.
If you’re using a CharacterController for the player, then make sure you are moving it using the Move/SimpleMove methods and not by directly moving the transform. It will probably help you to have a rigidbody on the player in any case, but you can turn off the physics for it by setting its isKinematic property to true.
You mean you set this in the 3D modelling app? Unity won’t recognise that - you have to add a MeshCollider component (menu Component > Physics > Mesh Collider).
If you can’t post the project, it would help if you would post a screen shot of the settings of one of the colliders that doesn’t work, a screen shot of the settings of your character controller and the code moving your player.
var turnSpeed = 10.0;
var moveSpeed = 10.0;
var mouseTurnMultiplier = 1;
private var x : float;
private var z : float;
function Update ()
{
// x is used for the x axis. set it to zero so it doesn't automatically rotate
x = 0;
// check to see if the W or S key is being pressed.
z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
// Move the character forwards or backwards
transform.Translate(0, 0, z);
// Check to see if the A or S key are being pressed
if (Input.GetAxis("Horizontal"))
{
// Get the A or S key (-1 or 1)
x = Input.GetAxis("Horizontal");
}
// Check to see if the right mouse button is pressed
if (Input.GetMouseButton(1))
{
// Get the difference in horizontal mouse movement
x = Input.GetAxis("Mouse X") * turnSpeed * mouseTurnMultiplier;
}
// rotate the character based on the x value
transform.Rotate(0, x, 0);
}
It uses physics, namely CharacterController.Move(). If you just set the position explicitly, it won’t work because that overrides any physics. Either modify the first person controller, or use one of the third person controllers from the Lerpz tutorial or others.
i have tried everything i can think of including editing the lerpz script etc. but i cant seem to get what i want.
here is my script -
var turnSpeed = 10.0;
var moveSpeed = 10.0;
var mouseTurnMultiplier = 1;
private var x : float;
private var z : float;
function Update ()
{
// x is used for the x axis. set it to zero so it doesn't automatically rotate
x = 0;
// check to see if the W or S key is being pressed.
z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
// Move the character forwards or backwards
transform.Translate(0, 0, z);
// Check to see if the A or S key are being pressed
if (Input.GetAxis("Horizontal"))
{
// Get the A or S key (-1 or 1)
x = Input.GetAxis("Horizontal");
}
// Check to see if the right mouse button is pressed
if (Input.GetMouseButton(1))
{
// Get the difference in horizontal mouse movement
x = Input.GetAxis("Mouse X") * turnSpeed * mouseTurnMultiplier;
}
// rotate the character based on the x value
transform.Rotate(0, x, 0);
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
}
the script works perfectly for controlling my player. its just that he walks through things.
can someone please tell me what i need to add/change to my script to get it to work?
Just getting the CharacterController component doesn’t move the object. The controller has a function called Move that you have to call to get the right collision effect. I’ve modified your script to show this, but I’ve left the transform.Translate line commented out to explain the difference.
var turnSpeed = 10.0;
var moveSpeed = 10.0;
var mouseTurnMultiplier = 1;
private var x : float;
private var z : float;
var controller: CharacterController;
function Start() {
// Get a reference to the controller
controller : CharacterController = GetComponent(CharacterController);
}
function Update ()
{
// x is used for the x axis. set it to zero so it doesn't automatically rotate
x = 0;
// check to see if the W or S key is being pressed.
z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
// Move the character forwards or backwards
// transform.Translate(0, 0, z);
controller.Move(new Vector3(0, 0, z);
// Check to see if the A or S key are being pressed
if (Input.GetAxis("Horizontal"))
{
// Get the A or S key (-1 or 1)
x = Input.GetAxis("Horizontal");
}
// Check to see if the right mouse button is pressed
if (Input.GetMouseButton(1))
{
// Get the difference in horizontal mouse movement
x = Input.GetAxis("Mouse X") * turnSpeed * mouseTurnMultiplier;
}
// rotate the character based on the x value
transform.Rotate(0, x, 0);
}