- Home /
Question by
13yearoldceo · Apr 07, 2015 at 09:04 PM ·
rigidbody2dvelocityphysics2dprojectiletransform.forward
how to make an 2d object move the direction its facing using rigidbody2d.velcoity
i use getcomponent().velocity = new vector2( transform.right,rigidbody2d>().velocity.y) but i get an error about invalid arguments with floats
Comment
Could you post your actual code? What you've got there won't compile and I can't tell what you are trying to do.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
public float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody2D>().AddForce(transform.right * speed ,Force$$anonymous$$ode2D.Force);
}
}
// i changed it to add force since i whouldnt compile but i would there use velocity ins$$anonymous$$d thank you
Answer by Piyush_Pandey · May 05, 2017 at 07:55 AM
Because transform.right
returns a Vector3
not a float
.
Use instead transform.right.x
A suggestion to optimize will be caching the components rigidbody and transform {if u are doing it on every frame}.