0
votes

I am trying to make my player jump but im getting an error in fixedUpdate with rb.AddForce "Cannot assign to AddForce because it is a method group" however it worked before i added the jump speed and jump function how can i make this work?

Code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

public float speed; //Creates Speed Variable
public Text countText; //Creates Count Text Variable
public Text winText; //Creates Win Text Variable
public float JumpSpeed;

private Rigidbody rb; //Creates Rigidbody Variable
private int count; //Creates Count Variable

void Start ()
{
    rb = GetComponent<Rigidbody>(); //sets variable for Rigidbody
    count = 0;
    SetCountText ();
    winText.text = "";
}

void FixedUpdate () //Controls
{
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    float moveForward = Input.GetAxis ("Forward");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce = (movement * speed);

    if (Input.GetKeyUp (KeyCode.Space)) 
    {
        JumpSpeed = 5.0f;
        Vector3 jump = new Vector3 (0.0f, moveForward, 0.0f);
    }

}

void OnTriggerEnter(Collider other) //Collect Items
{
    if (other.gameObject.CompareTag ("Pickup")) {
        other.gameObject.SetActive (false);
        count = count + 1;
        SetCountText ();
    }
}

void SetCountText() //Updates Count Text
{
    countText.text = "Count: " + count.ToString ();
    if (count >= 8) 
    {
        winText.text = "You Win!"; //Displays Win Text
    }
}

}
1
how is that different then rb,AddForce(movement * speed); bleow vector3 movement? Edit: nevermind remove the = is the difference - Nirset
Well, there is an additional = sign. That's not supposed to be there. = is the assignment operator. You are assigning the value of (movement * speed) to the method rb.AddForce, which, as I explain in my answer, does not make sense. If you want to call a method you have to use the () operator and put the parameters between them as shown in my answer. - Thomas Hilbert

1 Answers

1
votes

AddForce is a method, not a property. Methods have to be called, you don't assign them values. Try

rb.AddForce(movement * speed);

Other than that, maybe you should have a deeper look into programming basics. You can distinguish methods from properties, as method names usually start with a verb ("AddForce", "CreateObject" etc.), while properties' names describe, well, properties ("Color", "VerticalSpeed", "Name", ...)