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 apie2546 · Nov 16, 2015 at 08:41 AM · unity 5rigidbody2dapi

Unity Scripting API

Need help updating. I am following the Angry Bird Tutorial for my college class, and unfortunately in the tutorial online the guy doesn't provide the code that I can cross check with mine. I am getting multiple errors. I've attempted changing rigidbody2d to rb how unity 5 now classifies it but it gave me even more errors.

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
     public float maxStretch = 3.0f;
     public LineRenderer catapultLineFront;
     public LineRenderer catapultLineBack;
 
 
     private SpringJoint2D spring;
     private Transform catapult;
     private Ray rayToMouse;
     private Ray leftCatapultToProjectile;
     private float maxStretchSqr;
     private float circleRadius;
     private bool clickedOn;
     private Vector2 prevVelocity;
     public Rigidbody2D rb;
 
     void Awake () {
         rb = GetComponent<Rigidbody2D> ();
         spring = GetComponent <SpringJoint2D> ();
     }
 
     // Use this for initialization
     void Start () {
         LineRendererSetup ();
         rayToMouse = new Ray (catapult.position, Vector3.zero);
         leftCatapultToProjectile = new Ray (catapultLineFront.transform.position, Vector3.zero);
         maxStretchSqr = maxStretch * maxStretch;
         CircleCollider2D circle = Collider2D as CircleCollider2D;
         circleRadius = circle.radius;
     }
     
     // Update is called once per frame
     void Update () {
         if (clickedOn)
             Dragging ();
 
         if (spring != null) 
             if (!GetComponent<Rigidbody2D>().isKinematic && prevVelocity.sqrMagnitude > Rigidbody2D.velocity.sqrMagnitude){
             Destroy(spring);
             rb.velocity = prevVelocity;
         }
         if (!clickedOn) {
             prevVelocity = rb.velocity;
         
             LineRendererUpdate ();
         }
         else {
             catapultLineFront.enabled = false;
             catapultLineBack.enabled = false;
         }
     }
 
     void LineRendererSetup() {
         catapultLineFront.SetPosition (0, catapultLineFront.transform.position);
         catapultLineBack.SetPosition (0, catapultLineBack.transform.position);
 
         catapultLineFront.sortingLayerName = "foreground";
         catapultLineBack.sortingLayerName = "foreground";
 
         catapultLineFront.sortingOrder = 3;
         catapultLineBack.sortingOrder = 1;
     }
     void OnMouseDown (){
         spring.enabled = false;
         clickedOn = true;
     }
     void OnMouseUp (){
         spring.enabled = true;
         rb.isKinematic = true;
         clickedOn = false;
     }
     void Dragging () {
         Vector3 mouseWorldPoint = Camera.main.ScreenToViewportPoint (Input.mousePosition);
         Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
         if (catapultToMouse > maxStretchSqr){
             rayToMouse.direction = catapultToMouse;
 
         mouseWorldPoint.z = 0f;
         transform.position = mouseWorldPoint;
 }
     
 }
     void LineRendererUpdate () {
         Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
         leftCatapultToProjectile.direction = catapultToProjectile;
         Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circleRadius);
         catapultLineFront.SetPosition (1, holdPoint);
         catapultLineBack.SetPosition (1, holdPoint);
     }
 
 
 }


I am getting: Assets/Scripts/ProjectileDragging.cs(31,43): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected An object reference is required to access non-static member UnityEngine.Rigidbody2D.velocity' error CS0019: Operator >' cannot be applied to operands of type UnityEngine.Vector2' and float'

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 MelvMay · Nov 16, 2015 at 08:59 AM

As the error is indicating; Rigidbody2D is the type name of the component, it isn't a variable you can modify or query.

 if (!GetComponent<Rigidbody2D>().isKinematic && prevVelocity.sqrMagnitude > Rigidbody2D.velocity.sqrMagnitude)

  • 'Rigidbody2D.velocity.sqrMagnitude' should be 'rb.velocity.sqrMagnitude'

  • Not an error but 'GetComponent' should be 'rb' that you've already pre-fetched.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Anyone using STOMP and SockJS on Unity? 1 Answer

Cube Gravity - Apply Gravity to a Curve 0 Answers

Compilation error, Unity 5 0 Answers

How to add post from wordpress in unity? 0 Answers

Failed getting available Android API levels. Make sure your android sdk tools version is 25 or higher and you have internet connection. 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