- Home /
 
 
               Question by 
               PlayZzOfficial · Jul 16, 2021 at 11:35 PM · 
                c#physics3draycasthit  
              
 
              Diagonal Movement Question
When performing a physics.raycast to determine if something is hit, how would I go about overriding my diagonal movement with vertical or horizontal in the 3d space. my movement uses this,
 void Update()
     {
         movePlayer.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
         movePlayer.z = Input.GetAxisRaw("Vertical") * Time.deltaTime;
     }
     //-----//
     void FixedUpdate()
     {
         Move();
     }
     //-----//
     void Move()
     {
         var velocity = new Vector3(movePlayer.x, 0, movePlayer.z).normalized * movementVelocity;
 
         velocity.y = playerRigidbody.velocity.y;
         playerRigidbody.velocity = velocity;
     }
 
               I only want to override the diagonal if I hit something in that direction.
               Comment
              
 
               
              Answer by PlayZzOfficial · Jul 17, 2021 at 01:29 AM
So I watched Code monkies video, but my horizontal movement, wont work im throwing 2 different raycasts for some reason.
  void FixedUpdate()
     {
         Move();
         Jump();
     }
     //-----//
     void Move()
     {
         RaycastHit sensor1;
 
         
 
         Vector3 movement = new Vector3(moveDirection.x, 0, moveDirection.z).normalized;
 
         Vector3 targetPostion = movement * movementVelocity;
         Physics.Raycast(transform.position, moveDirection, out sensor1, 1);
         Debug.DrawLine(transform.position, sensor1.point, Color.red);
         targetPostion.y = playerRigidbody.velocity.y;
         playerRigidbody.velocity = targetPostion;
         if (sensor1.collider == null)
         {
             lastMoveDirection = moveDirection;
             playerRigidbody.velocity = targetPostion;
             Debug.Log("Haven't Hit");
         }
         else
         {
             Vector3 testingDirection = new Vector3(moveDirection.x = 0, 0, moveDirection.z).normalized;
             targetPostion = testingDirection * movementVelocity;
             Physics.Raycast(transform.position, testingDirection, out sensor1, 1);
             Debug.DrawLine(transform.position, sensor1.point, Color.red);
             Debug.Log("Hitting Things");
             if(testingDirection.z != 0 && sensor1.collider == null)
             {
                 lastMoveDirection = testingDirection;
                 playerRigidbody.velocity = targetPostion;
                 Debug.Log("Haven't Hit");
             }
             else
             {
                 testingDirection = new Vector3(moveDirection.x, 0, moveDirection.z = 0).normalized;
                 targetPostion = testingDirection * movementVelocity;
                 Physics.Raycast(transform.position, testingDirection, out sensor1, 1);
                 Debug.DrawLine(transform.position, sensor1.point, Color.red);
                 Debug.Log("Hitting Things");
                 if (testingDirection.x != 0 && sensor1.collider == null)
                 {
                     lastMoveDirection = testingDirection;
                     playerRigidbody.velocity = targetPostion;
                     Debug.Log("Haven't Hit");
                 }
             }
         }     
     }
 
              Your answer