Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dogg · Mar 19, 2016 at 09:49 AM · c#2drotationphysicsraycasthit2d

How To Fix My RayCastHit2D in Unity 4.3.4?

Hello! I'm trying to have my 2D Player rotate towards the direction that the 2D terrain is pointing, but I'm having a problem. It only rotates/adjusts to the terrain when I jump while on the terrain. It does not rotate when I'm just moving along it. For my collision I just have 2D box colliders attached to my player along with a rigidbody2D. I don't think that's the problem since I can clearly see the colliders touching the ground. So I'm looking for some help, does anyone know what could be the problem? Long code that I wrote so I'm sorry for this: ////// = related to Raycast.

Here's my code:

 [SerializeField]
 GameObject TrackObject; //The Player(this game object) tracks rotation of this object in another script
 [SerializeField]
 Transform TerrainRot; //An empty game object that TrackObject follows rotation of.
 
 public BoxCollider2D jumpNorm;
 public BoxCollider2D jumpNorm2;
 
 public BoxCollider2D CollideJump;
 public BoxCollider2D CollideJump2;
 
 public bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 1.0f; //was 0.2f
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
 public Animator anim;
 
 void Awake()
     {
         anim = anim.GetComponent<Animator>();
     }
 
 void FixedUpdate()
 {
 grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 anim.SetBool ("Ground", grounded);
         
 anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
 anim.SetFloat ("Speed", rigidbody2D.velocity.x);
 
         if (!grounded)
             return;
 
 }
 
 void Update()
     {
         TrackObject.transform.rotation = TerrainRot.rotation; ////////
 
         RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up); ////////
         Debug.DrawRay(transform.position, -Vector2.up, Color.green); ///////
 
         if(hit.collider.tag == "Respawn") /////////
             {
                 Debug.Log("DynamicGroundHit");
             TerrainRot.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; ////////
             }
 
         if(hit.collider.tag == "Ground") //////
         {
             Debug.Log("GroundHit");
             TerrainRot.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; ////////
 
 if (AllowJump) {
         if (grounded && Input.GetMouseButtonDown (0)) {
                 jumpNorm.enabled = false;
                 jumpNorm.enabled = false;
 StartCoroutine (JumpCollision ());
                 anim.SetBool ("Ground", false);
                 rigidbody2D.AddForce (new Vector2 (0, jumpForce));
                         }
         }
 }
 
 IEnumerator JumpCollision()
     {
         yield return new WaitForSeconds (0.1f); 
         CollideJump.enabled = true;
         CollideJump2.enabled = true;
         StartCoroutine (CollideJump ());
     }
 
 
     IEnumerator CollideJump()
     {
         yield return new WaitForSeconds(0.7f);
         CollideJump.enabled = false;
         CollideJump2.enabled = false;
         jumpNorm.enabled = true;
         jumpNorm2.enabled = true;
     }
 
 


Comment
Add comment · Show 5
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 Oukalakakou · Mar 19, 2016 at 12:14 PM 0
Share

$$anonymous$$aybe your RayCast is send from the foot of the player that means exactly on the terrain and that's why it doesn't detect it? It could explain why it only works when jumping.

avatar image Dogg Oukalakakou · Mar 20, 2016 at 06:22 AM 0
Share

Thank you for replying! I think it's definitely a possibility. How can I test it or fix it? I've tried putting the raycast on an empty game object attached to the player, and changing it's position to see if that was the actual problem, but the results were not what I wanted.

I really wish I could use raycasthit2D distance(as that could be my problem), but my Unity version is too old.

avatar image Dogg Dogg · Mar 20, 2016 at 06:29 AM 0
Share

To anyone who reads this post: I've seen people use a float|int on a 3D raycast to change the distance of where the raycast hits. For an example I found this code: if (Physics.Raycast(ray, out hit, 2)) The number "2" is the distance. Is it possible to do this with my 2D code here?

Show more comments
avatar image Dogg · Mar 21, 2016 at 01:39 AM 0
Share

@Oukalakakou No i think I misunderstood it's purpose, sorry about that. But good news I found a way to make the raycast do what I want. I took some of the code from my Jump code, and put it in the FixedUpdate function. It now looks like this:

 void FixedUpdate ()
     {
         jumper.enabled = false;
         jumper2.enabled = false;
         StartCoroutine (CrocJumperCollision ());
 }

Although this works(and it makes my character float due to the colliders turning on/off, which I don't $$anonymous$$d), it must be highly inefficient due to running almost all the time. It also caused my game to crash on mobile. Any idea why it works, and is there a better way to do this? Thank you for your help.

0 Replies

· Add your reply
  • Sort: 

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

143 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 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 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 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 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 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 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 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 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

C# Water Rotation 0 Answers

Flip over an object (smooth transition) 3 Answers

RayCast2D direction is always showing origin point(0,0,0) on game and ignores direction Vector2D.(Unity 2017.4.7.f1) 0 Answers

How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers

sprite refuses to flip in a specific angle, please help 0 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