Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AronChan · Nov 07, 2013 at 12:57 PM · c#collisionphysicsrigidbodyraycast

Raycast doesnt detect object in front of rigidbody (player is stuck on wall)

I know this question has been asked quite frequently but any of the other threads havent quite seemed to settle this issue for me.

My player is controlled by physics and a rigidbody. When he collides with a wall mid jump and continuesly applies forward motion he will be stuck on the side of said wall.

I figured my solution to this problem would be to reset his velocity towards the wall but something is wrong with my raycast since my player never detects the wall.

!! Update: I did some debugging with the raycast and it seems to me that it is only cast when im standing still. Not really sure why.

If somebody could comment on my (newbie) code and help me find out what my error is that would be greatly appreciated.

My player has a mass of 1, he is scaled at 0.1 on all axis.

this is how he moves:

 [RequireComponent(typeof(PlayerPhysics))]
 public class PlayerController : MonoBehaviour {
     
     private float walkSpeed = 4;
     private float runSpeed = 8;
     public float acceleration = 30;
     private Vector3 jumpHeight;
     
     private float distToGround;
     
     private float targetSpeed;
     private float currentSpeed;
     private Vector3 amountToMove;
     
     private PlayerPhysics playerPhysics;
     
     //Playerstates
     private bool isGliding;
     
     // Use this for initialization
     void Start () {
         playerPhysics = GetComponent<PlayerPhysics>();
         jumpHeight = new Vector3(0, 8, 0);
         distToGround = collider.bounds.extents.y;
     }
      
     // Raycast under the player to detect if he is grounded
     private bool IsGrounded()
     {
           return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
     }    
     
     // Update is called once per frame
     void Update () {
         // Choppermode During jump
         if (isGliding)
         {
             rigidbody.drag = 10;
         }
         else
         {
             rigidbody.drag = 0;
         }
     }
     
     void FixedUpdate () {
         // Player Movement
         
         
         #region Player Movement
         float speed = (Input.GetButton("Run"))?runSpeed:walkSpeed;
         
         targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
         
         currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);
         
         amountToMove.x = currentSpeed;
         
         playerPhysics.Move(amountToMove);
         #endregion
         
         // Player Jumping
         if (!IsGrounded() && Input.GetButton("Glide"))
         {
             isGliding = true;
         }
         else
         {
             isGliding = false;
         }
         
         // Player Jump
         if (IsGrounded() && Input.GetButtonDown("Jump"))
         {
             rigidbody.AddForce(jumpHeight, ForceMode.Impulse);
         }
     }
     
     // Increase n towards target by speed
     private float IncrementTowards(float n, float target, float a) {
         if (n == target) {
             return n;    
         }
         else {
             float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
             n += a * Time.deltaTime * dir;
             return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
         }
     }
 }

this is how i cast my ray to detect objects in front of him:

    using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(BoxCollider))]
 public class PlayerPhysics : MonoBehaviour {
         
     private BoxCollider collider;
     private Vector3 s;
     private Vector3 c;
     
     private Vector3 originalSize;
     private Vector3 originalCentre;
     private float colliderScale;
     
     private int collisionDivisionsY =10;
     
     private float skin = .01f;
     
     Ray ray;
     RaycastHit hit;
     
     // Use this for initialization
     void Start () {
         collider = GetComponent<BoxCollider>();
         colliderScale = transform.localScale.x;
         
         originalSize = collider.size;
         originalCentre = collider.center;
         SetCollider(originalSize,originalCentre);
     }
     
     public void Move(Vector3 amountToMove)
     {
         float deltaX = amountToMove.x;
         Vector2 p = transform.position;
         
         // Check collisions left and right
         for (int i = 0; i<collisionDivisionsY; i ++) 
         {
             float dir = Mathf.Sign(deltaX);
             float x = p.x + c.x + s.x/2 * dir;
             float y = p.y + c.y - s.y/2 + s.y/(collisionDivisionsY-1) * i;
             
             ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
             Debug.DrawRay(ray.origin,ray.direction);
             
             if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin)) 
             {
                 // Get Distance between player and object on xaxis
                 float dst = Vector2.Distance (ray.origin, hit.point);
                 
                 // Initiate players movement according to collision
                 if (dst > skin) 
                 {
                     Debug.Log("should be walkin");
                     rigidbody.MovePosition(rigidbody.position + amountToMove * Time.deltaTime);
                 }
                 else 
                 {
                     Debug.Log("should stop");
                     rigidbody.MovePosition(rigidbody.position + new Vector3(0, amountToMove.y, 0) * Time.deltaTime);
                     
                 }
                 break;
             }
         }
     }
     
     // Set collider
     public void SetCollider(Vector3 size, Vector3 centre) {
         collider.size = size;
         collider.center = centre;
         
         s = size * colliderScale;
         c = centre * colliderScale;
     }
     
     public void ResetCollider() {
         SetCollider(originalSize,originalCentre);    
     }
 }
 
         
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mattssonon · Nov 07, 2013 at 02:24 PM

Are you sure you want to cast the ray in the direction Vector3.forward? Try changing it to transform.forward so it follows the object's forward direction.

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 AronChan · Nov 07, 2013 at 03:43 PM 0
Share

Hi. Thank you for looking at my code. Unfortunately that was not the issue here. Ive updated the way my collision is detected to be more precise but im still having the same issue.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't Make Raycast Target Nearest Character 1 Answer

Why can't my enemies walk up an incline? 1 Answer

Restrict two objects from getting too close. 1 Answer

airplane collision detection problem 1 Answer

Surface with hole and Raycast - Which collider 1 Answer


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