- Home /
How to remove z axis control from script
Hey, I have a basic enemy that patrols between two or more points, my game is a side scroller and Id like not to be able to push him forward/backward on the Z axis, causing him to fall off the platform. heres the part of the script that controls patrolling;
if( grounded )
{
if(CurrentWayPoint < WayPoint.length)
{
var target : Vector2 = WayPoint[CurrentWayPoint].position;
var moveDirection : Vector2 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
CurrentWayPoint++;
}
else
{
velocity = moveDirection.normalized * Speed;
}
}
else
{
if( loop )
{
CurrentWayPoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
}
this is in the Update function and 'grounded' is if it is falling or not. How and where would I put something like transform.position.z = 0;
???
Answer by anonymous 2 · Apr 21, 2011 at 03:06 AM
In the editor select your prefab or gameobject enemy and under the inspector in your rigidbody component click on constraints. You can freeze rotation and position on all the Axis. You could also try adding transform.position.z = 0; to your update function like this :
function Update () {
transform.position.z = 0;
if( grounded )
{
if(CurrentWayPoint < WayPoint.length)
{
var target : Vector2 = WayPoint[CurrentWayPoint].position;
var moveDirection : Vector2 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
CurrentWayPoint++;
}
else
{
velocity = moveDirection.normalized * Speed;
}
}
else
{
if( loop )
{
CurrentWayPoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
}}
In the inspector panel when you click on your enemy there should be a rigid body. In the bottom part of the rigid body there should be the word constraint. Click on it and you should see two rows of three. Click on z in the lower row.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
problem with my script need help 2 Answers
Help with Patrol Script!! 1 Answer
AI patrolling script 1 Answer