- Home /
Question by
ninjalemur · Oct 18, 2017 at 07:07 PM ·
scripting problemif statementscriptingproblemcollison
How to make this script only wall jump on certain walls?
void Start() {
}
public float GroundSpeed = 3f;
public float GravityStrength = 5f;
public float JumpSpeed = 10f;
public float AerialSpeed = 2f;
float verticalvelocity;
public Transform CameraTransform;
bool canJump = false;
Vector3 Velocity;
Vector3 groundedVelocity;
Vector3 normal;
bool onWall = false;
bool CollidingWall = false;
// Update is called once per frame
void Update()
{
Vector3 myVector = Vector3.zero;
Vector3 input = Vector3.zero;
//Get input from the player
input.x = Input.GetAxis("Horizontal");
input.z = Input.GetAxis("Vertical");
input = Vector3.ClampMagnitude(input, 1f);
Quaternion inputRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(CameraTransform.forward, Vector3.up), Vector3.up);
input = inputRotation * input;
if (MyController.isGrounded)
{
myVector = input;
myVector *= GroundSpeed;
}
else
{
myVector = groundedVelocity;
myVector += input * AerialSpeed;
}
myVector = myVector * Time.deltaTime;
myVector = Vector3.ClampMagnitude(myVector, GroundSpeed);
verticalvelocity = verticalvelocity - GravityStrength * Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
if (onWall)
{
Vector3 reflection = Vector3.Reflect(Velocity, normal);
Vector3 projected = Vector3.ProjectOnPlane(reflection, Vector3.up);
groundedVelocity = projected.normalized * GroundSpeed+normal*AerialSpeed;
}
if (canJump)
verticalvelocity += JumpSpeed;
}
myVector.y = verticalvelocity * Time.deltaTime;
//Use input to move the player
CollisionFlags flags = MyController.Move(myVector);
Velocity = myVector / Time.deltaTime;
if ((flags & CollisionFlags.Below) != 0)
{
groundedVelocity = Vector3.ProjectOnPlane(Velocity, Vector3.up);
verticalvelocity = -3f;
canJump = true;
onWall = false;
}
else if ((flags & CollisionFlags.Sides) != 0) {
canJump = true;
onWall = true;
}
else
{
canJump = false;
onWall = false;
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{ normal = hit.normal;
}
};
How would I make it so that this script only allows you to wall jump on certain walls? I've tried but I could find no solution.
Comment
Answer by AaronBacon · Oct 18, 2017 at 08:53 PM
You can assign a tag in the editor to any walls you want to be able to jump off and then use the function OnCollisionEnter("WallJumpTag") To only run code if you just hit a wall that can be jumped off.