Question by
DoomPriestK · Dec 02, 2019 at 12:40 AM ·
c#gameobject
Convert Float to Initial Force
So i'm adding script to my projectile so it will look at the mouse cursor position, turn to face it then apply an initial burst of force set by the player in the inspector, after which gravity will take hold. I get error CS0103: The name 'AddForceAtPosition' does not exist in the current context, I can't seem to see a way around without sacrificing the ability to set the speed in the inspector (which is important)
[CODE]
void Start()
{
theRB = GetComponent<Rigidbody2D>();
gameObject.layer = myLayer;
Physics2D.IgnoreLayerCollision(myLayer, myLayer, true);
// GETS WHERE THE CURSOR IS ON THE SCREEN
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// LOOK TOWARDS THE CURSOR
transform.LookAt(Vector3.zero);
// THEN ADD INITIAL VELOCITY
theRB = AddForceAtPosition(nutSpeed);
[/CODE]
Comment
Answer by lodendsg · Dec 02, 2019 at 08:00 AM
Change the last line there to
theRB.AddForceAtPosition(nutSpeed);
Add Force at Position is a member of Rigidbody not GameObject https://docs.unity3d.com/ScriptReference/Rigidbody.AddForceAtPosition.html
Answer by TheHobbyist · Dec 02, 2019 at 02:07 PM
You need to call it like:
theRB.AddForceAtPosition(force, position);
Where both force and position are Vector3 objects.