- Home /
Question by
iR3dyPlayZ · Jul 11, 2021 at 05:28 PM ·
c#movementraycastvoidrestrictmovement
Void Execution Question
So I'm trying to stop my void from executing when it meets a certain expectation, so I want
void Update()
{
moveForward.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
moveForward.z = Input.GetAxisRaw("Vertical") * Time.deltaTime;
}
//-----//
void FixedUpdate()
{
Move();
}
//-----//
void Move()
{
var velocity = new Vector3(moveForward.x, 0, moveForward.z).normalized * movementVelocity;
velocity.y = playerRigidbody.velocity.y;
playerRigidbody.velocity = velocity;
}
to have different restrictions, like no diagonal movement but when pressing keys like (W,D) to move up when against an object on the right or (W,A) to do the same thing but against an object on the Right. Also I want my raycast to extend from all 4sides
this is the void that will handle that
void ObjectDetection()
{
Physics.Raycast(playerRigidbody.position, moveForward, out RaycastHit objectDetection, 1);
if (objectDetection.collider == null)
{
// player can move without any issue, but I have a move void already so how would I implament a command allowing me to move when i already call upon the move void function in the fixed update??
//print("Haven't Hit Anyhing");
}
else
{
if (objectDetection.collider != null)
{
// Player cannont move in diagnol and cannont keep moving twords object. But player can move along the wall even when hold the diagnol it will be overridden by moving verticaly or horizontally, following the wall.
print("Hit Something");
Debug.Log(objectDetection.transform.name);
}
}
}
So when I'm not hitting anything I have full 8 directional movement, but when I do hit something I have 4 directional movement but I can still use two keys when I'm up against something but to move left or right up or down depending on the walls placement.
Comment