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 /
  • Help Room /
avatar image
0
Question by mike1233 · Feb 04, 2017 at 12:40 PM · scripting problem2d game2d-gameplay

i updated my unity 5. and now getting errors

this script was working fine in older unity version now it has errors and no scenes will play

its 2d so why is error asking about rigidbody 2d its definitely a 2d game

error is Assets/Standard Assets/Scripts/General Scripts/DragRigidbody.js(30,18): BCE0019: 'rigidbody2D' is not a member of 'UnityEngine.RaycastHit'. Did you mean 'rigidbody'?

 var spring = 50.0;
 var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var attachToCenterOfMass = false;
 
 var  Idle: AnimationClip;
 private var springJoint : SpringJoint;
 
 function Update ()
 {
 
 if (Mathf.Abs(Input.GetAxis("Vertical")) <= 0.1){
         GetComponent.<Animation>().CrossFade("Idle");
     }
 
     // Make sure the user pressed the mouse down
     if (!Input.GetMouseButtonDown (0))
         return;
 
     var mainCamera = FindCamera();
         
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 100))
         return;
     // We need to hit a rigidbody that is not kinematic
     if (!hit.rigidbody2D || hit.rigidbody2D.isKinematic)
         return;
     
     if (!springJoint)
     {
         var go = new GameObject("Rigidbody2D dragger");
         var body : Rigidbody2D = go.AddComponent.<Rigidbody2D>() as Rigidbody2D;
         springJoint = go.AddComponent.<SpringJoint>();
         body.isKinematic = true;
     }
     
     springJoint.transform.position = hit.point;
     if (attachToCenterOfMass)
     {
         var anchor = transform.TransformDirection(hit.rigidbody2D.centerOfMass) + hit.rigidbody2D.transform.position;
         anchor = springJoint.transform.InverseTransformPoint(anchor);
         springJoint.anchor = anchor;
     }
     else
     {
         springJoint.anchor = Vector3.zero;
     }
     
     springJoint.spring = spring;
     springJoint.damper = damper;
     springJoint.maxDistance = distance;
     springJoint.connectedBody = hit.rigidbody2D;
     
     StartCoroutine ("DragObject", hit.distance);
 }
 
 function DragObject (distance : float)
 {
     var oldDrag = springJoint.connectedBody.drag;
     var oldAngularDrag = springJoint.connectedBody.angularDrag;
     springJoint.connectedBody.drag = drag;
     springJoint.connectedBody.angularDrag = angularDrag;
     var mainCamera = FindCamera();
     while (Input.GetMouseButton (0))
     {
         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
     }
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
 }
 
 function FindCamera ()
 {
     if (GetComponent.<Camera>())
         return GetComponent.<Camera>();
     else
         return Camera.main;
 }
 

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
1

Answer by HenryStrattonFW · Feb 04, 2017 at 04:54 PM

The error indicates that you are attempting to pull a variable called rigidbody2D from a RaycastHit object, RaycastHit does not contain a variable by that name, it does contain a variable called "rigidBody" but this is of Type RigidBody if you are making a 2D game you may need to be using the 2D physics raycasting instead.

https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

This will give you back the hit object as a RaycastHit2D which has a variable of rigidBody but this time of type "RigidBody2D".

Comment
Add comment · Show 2 · 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 mike1233 · Feb 04, 2017 at 06:40 PM 0
Share

I see -

I t was working perfectly before I updated unity to 5.5 so how would I correct it I checked the ref link out but its very confusing

avatar image HenryStrattonFW mike1233 · Feb 04, 2017 at 10:58 PM 0
Share

Perhaps the separation of the 2D physics into its own namepsace was a new change in that version. Correcting it would depend on your code, it may be as simple as locating all the places where you are using Physics.Raycast and swapping them out for calls to Physics2D.Raycast (you may have to swap Vector3 parameters for Vector2 here as well). then when you're trying to access the rigidbody just use "rigidbody" ins$$anonymous$$d of "rigidbody2d"

LIke I said the extent of changes depends entirely on your project and how many areas are effected by this change.

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

116 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

Related Questions

My character won't stop crouching, they dont stop crouchin 2 Answers

Player should turn in the direction the player is running. (2D Game) 0 Answers

Unity 2D 5.2.2 How can I aim with keys towards an object? 2 Answers

2d platformer physics 0 Answers

How can I program a select level Map like the Super mario bro 3 or Shovel Knight? 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