- Home /
How would I restrict Movement in Air?
I made a Top-down 3D Movement Script where you intentionally can't Jump. But I also don't want, that you can walk in Air. When you're in Air, you should just fall.
This Video shows, how you can move in the Game. And around second 14, it starts showing how you're supposed to be moving. https://youtu.be/Zm2k2PF8BCQ
This is the Script I need help for restricting Movement in Air: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_Move : MonoBehaviour
{
//Movement Variables (Next 3 Code Lines):
public float walkSpeed;
private float xInput;
private float yInput;
//Floor Check Variables (Next 4 Code Lines):
public Transform floorCheck;
public float floorCheckRadius;
public LayerMask floorLayer;
private bool isOnFloor;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Floor Check Code:
isOnFloor = Physics.OverlapSphere(floorCheck.position, floorCheckRadius, floorLayer).Length > 0;
//Code to get Input for Player Movement (Next 3 if-Statements):
if (isOnFloor = true) //Only check for Input if Character is on Floor
{
if (Input.GetAxisRaw("Horizontal") != 0)
{
xInput = Input.GetAxisRaw("Horizontal");
yInput = 0;
} else
if (Input.GetAxisRaw("Vertical") != 0)
{
yInput = Input.GetAxisRaw("Vertical");
xInput = 0;
}
}else //Hinder Input if Character is not on Floor (This else-Statement):
{
xInput = 0;
yInput = 0;
}
transform.Translate (xInput * walkSpeed, 0, yInput * walkSpeed); //Make the Player Walk according to Input
//Code to restrict diagonal Movement (Next 2 if-Statements):
if(Input.GetAxisRaw("Horizontal") == 0)
{
xInput = 0;
}
if (Input.GetAxisRaw("Vertical") == 0)
{
yInput = 0;
}
}
}
$$anonymous$$ight want to change:
if (isOnFloor = true)
to
if (isOnFloor == true) // or just if (isOnFloor)
variable assignment does return a value, so that's legal syntax and your the compiler won't tell you about it.
Answer by DehWonderSquid · Feb 27, 2020 at 02:16 PM
You can use isOnFloor to see if its false and have it reduces speed by like *0.90. Then when it returns true it 'll change the speed again.
Your answer
Follow this Question
Related Questions
How can i make my character jump and slide on command? 0 Answers
My movement script is kind of broken for jumping (upward force is diminished after first jump) 1 Answer
sonic movement stick to the ground 1 Answer
How to make a top down character snap to different directions 0 Answers
Want to make GameObject follow another GameObject's path Unity3D 1 Answer