Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Qualdan · Jun 05, 2012 at 02:28 PM · c#raycastfpsfalling

Raycasting to avoid falling (FPS game)

I’m trying to make it so that the player won’t be able to walk into a place where he would fall. Meaning I want to do a raycast into the direction in which the player is moving (which isn’t necessarily forward as the player might for example strafe) and if there is a fall of more than 1 unit (no need to be this precise), prevent movement to that direction.

Due to the sheer amount of level geometry, I cannot use colliders for this. Any help would be appreciated (preferably in C#).

I searched for a similar solution and I found this thread: “Raycast to check for potential pitfalls” (http://forum.unity3d.com/threads/138406-Raycast-to-check-for-potential-pitfalls), but I seem to be unable to modify it to suit my needs.

Acknowledgement: As a basis for this script I am using “FPSWalkerEnhanced” (http://unifycommunity.com/wiki/index.php?title=FPSWalkerEnhanced) by Eric Haines (Eric5h5).

A side note: I copied (or at least tried to copy) only the relevant parts from the script to this post, since the script itself is exceedingly long (with a lot of actions which have nothing to do with player movement). So if it seems there is an obvious omission, that’s probably the reason.

 void Update() {
 
     // Get axis
 float inputX = Input.GetAxis("Horizontal");
 float inputY = Input.GetAxis("Vertical");
 
     // Prevent sidestepping speed increase
 float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f)? .7071f : 1.0f;
 
 // Determine if player is moving
 if(oldPos.x != transform.position.x || oldPos.z != transform.position.z) {
     isMoving = true;
 }
 else {
     isMoving = false;
 }
 oldPos = transform.position;
 
 // If player is touching the ground        
 if (grounded) {
     // If player was falling
     if (falling) {
         falling = false;
 
         // Give damage if fallen for too much
         if (myTransform.position.y < fallStartLevel - fallingDamageThreshold) {
             FallingDamageAlert (fallStartLevel - myTransform.position.y);    
         }
     }
 
     // Movement direction
     moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
     moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
     moveDirection = myTransform.TransformDirection(moveDirection) * speed;
 
 
         // Jumping
         if (Input.GetButtonDown("Jump")) {
             Jump();
         }
 }
 else {
     // If player was not falling
     if (!falling) {    
         falling = true;
 
         // Determine start position of fall
         fallStartLevel = myTransform.position.y;
     }
 
     // If airControl is allowed, allow movement while on air
     if (airControl) {
         moveDirection.x = inputX * speed * inputModifyFactor;
         moveDirection.z = inputY * speed * inputModifyFactor;
         moveDirection = myTransform.TransformDirection(moveDirection);
     }
 }
 
 
 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;
 
 // Move the player
 grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Loius · Jun 05, 2012 at 06:04 PM

Just set your ray start point to player.position + player.GetPlanarVelocity() * (an amount of time).

Or you can make it a constant buffer distance from 'pits' by using player.position + player.GetPlanarVelocity.normalized() * (a distance).

Looks like your 'moveDirection' would work as velocity -

function GetPlanarVelocity() : Vector3 { return new Vector3(moveDirection.x, 0, moveDirection.z); }

Shoot your ray straight down from that point. If it can travel farther than [1 meter] then it's a 'pit'.

If you can 'hover' above safe ground, you might need to raycast straight down from player to find the floor, then nudge over like above, then raycast straight down from the new point.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qualdan · Jun 05, 2012 at 06:45 PM 0
Share

I was able to use your advice as a starting point (thanks for that). But now another problem has risen: when the player reaches the edge (and the game properly deter$$anonymous$$es there is a "pit"), he cannot move anymore. Not in any direction (unless I tick off the DownAllowFall).


if (DontAllowFall) {
    RaycastHit groundCheck;                
    Vector3 moveWhere = new Vector3(moveDirection.x, 0, moveDirection.z);
    if (!Physics.Raycast(myTransform.position + moveWhere * 1, Vector3.down, out groundCheck, 5)) {
        print("NO GROUND");
        moveDirection = new Vector3(0, 0, 0);
    }
    else {
        print("GROUND");                    
        moveDirection = new Vector3(inputX * input$$anonymous$$odifyFactor, -antiBumpFactor, inputY * input$$anonymous$$odifyFactor);
        moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
        moveDirection = myTransform.TransformDirection(moveDirection) * speed;    
    }
}
else {
    moveDirection = new Vector3(inputX * input$$anonymous$$odifyFactor, -antiBumpFactor, inputY * input$$anonymous$$odifyFactor);
    moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;    
}

avatar image Loius · Jun 05, 2012 at 07:07 PM 0
Share

What is moveDirection co$$anonymous$$g into this? You need to use mD after it's been translated to local space (TransformDirection) or it won't line up right.

You may also need to only do the check if moveWhere.magnitude > 0

You should be able to say "if no ground then stop player" and that's it.

avatar image Qualdan · Jun 05, 2012 at 07:17 PM 0
Share

Ok, that did it. You sir, are a genius. Thank you.

avatar image
0

Answer by GutoThomas · Jun 05, 2012 at 04:01 PM

Why don't you just add some colliders in an empty game object and position them in the places where the player won't be able to walk?

I guess is a pretty quick solution and will not require Raycast, which isn't the most light thing for the CPU.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qualdan · Jun 05, 2012 at 05:29 PM 0
Share

Like I said, "Due to the sheer amount of level geometry, I cannot use colliders for this". It is just not feasible to add colliders everywhere. And that would also make it impossible for the player "jump up" to a place if there is a collider blocking.

But thanks for the input. It just not feasible solution for me.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Modifing an Enum value by a String? 2 Answers

Ray.direction editing not working in C#? 1 Answer

Where can i find an easy raycast shooting tutorial for C#? 2 Answers

shooting multiple enemies using raycast 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges