Few questions about this script..

Hey, i have made a sidescrolling level and its extremely important that i wont move a cm in the Z axis.
i have “disabled” the Z axis in the script and the player is not effected by cherecter controller, but its effected by rigidbody, so if the floor is not stright, the player might slide in the Z axis…
im using terrain and its a “cave” level so the floor has to be like that…
script:

var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
var dead = false;
private var grounded = false;

@script RequireComponent(Rigidbody, CapsuleCollider)

function Awake ()
{
    rigidbody.freezeRotation = true;
    rigidbody.useGravity = false;
}

function FixedUpdate ()
{
if(dead)
{
transform.position = gameObject.Find("CheckPoint").transform.position;
dead = false;
}
        // Calculate how fast we should be moving
        var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
        //targetVelocity = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;
        
        // Apply a force that attempts to reach our target velocity
       var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);

        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
		velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

	if (grounded)
   {	
        // Jump
        if (canJump  Input.GetButton("Jump"))
        {
            rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), 0);
        }
    }
    
    // We apply gravity manually for more tuning control
    rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    
    grounded = false;
}
function OnTriggerEnter(hit:Collider)
{
if(hit.gameObject.tag == "fallout")
{
dead = true;
}
if(hit.gameObject.tag == "CheckPoint")
{
Destroy(hit.gameObject);
gameObject.Find("CheckPoint").transform.position = hit.transform.position;
}
if(hit.gameObject.tag == "Respawn")
{
transform.position = Vector3(146.3705,9.376331,1920.817);
}
}
function OnCollisionStay ()
{
    grounded = true;    
}

function 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);
}

and the 2nd problem is, my level has platforms that move the player left and right but whenever the player is on them, it doesnt follow them, it stay and follow by a littile distence untill it falls.
the problem is with the “MaxVelocityChange”
but thers a problem to turn it to a lower value than 1 because my player wont move at all.
Thanks to the helpers :wink:

I don´t think I understood your first problem, but about the second one, just make your player´s transform equal to the plataform´s transform. Inside your collision function, test if the player has collided with a plataform and add this

player.transform.parent = plataform.transform;

Don´t forget to set player.transform.parent to null after he leaves the plataform.

There ara many posts about this moving plataform problem. Search the forum and you´ll find whole scripts to handle this.

thanks for the replay, well ill try to explain again the first one…
if you have a rigidbody without a cherecter controller and the floor is not flat, your player will slide according to the floor angle.
in my level, the floor is not really flat, and the player in some parts, slide to the Z axis and thats really bad because than, if it slides to much, i cant jump to the platforms and other stuff
uhm i attached a picture showing what i mean if it was still uncleared.
bottom line: lock the Z axis so that my player wont be able to go there even ifi ts effected by the floor.[/img]

I have three “ugly” suggestions if you want some kind of quick fix:

  1. Add a check on your transform.position.z and if it’s < or > to a given value, lerp it back to the ideal z position (0 ?).

  2. Make your platforms larger to add a bit of tolerance.

  3. When you jump (for example), snap the character back to the proper z value.

There is probably a better solution somewhere though.

somewhere along in the function include this by itself (that function FixedUpdate() will do or you could add a function Update() to save on processing calls)

transform.position = zLock;

and put this above the functionvar zLock : float = 0.0; this should make sure the player always stays on the z axis at whtever z co-ordinate you set in the zLock in the inspector 8)

thanks but whenever i put this, i get the error:
“Cannot convert ‘float’ to unityEngine.vector3”
what should i do?

could you post the full error and the code your using?

try:

transform.position.z = zLock;

Woops :sweat_smile: i cant believe i forgot .z seriously lol
yup that will certainly fix it.

Thanks! that works :wink:

ugh lol im sorry for all these replays but its still doesnt work.
now when im on it, i cant walk
any solutions?..

The best way to confine a rigidbody to a plane is to add a configurable joint to it. You just need to set the joint’s ZMotion property to Locked and enable the Configured In World Space option.

Thanks that worked :wink: