- Home /
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'
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.
Your answer
Follow this Question
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