locked
Can you fix my code? RRS feed

  • Question

  • It's a pretty simple script but for some reason it keeps saying: Unexpected symbol ' speed' 

    using UnityEngine;
    using System.Collections;

    public class playercontroller : MonoBehaviour
    {
    public float speed;

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

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

    rigidbody.AddForce(movement " speed " Time.deltatime);
    }
    }

    • Moved by Carl Cai Tuesday, November 25, 2014 12:04 PM more related to suggested forum
    Saturday, November 15, 2014 5:41 PM

Answers

  • The rigidbody.AddForce method takes three float parameters and an optional ForceMode or a Vector3 and an optional ForceMode: http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    Since your speed field is never used, I assume you want to pass the Vector3 to the AddForce method:

    using UnityEngine;
     using System.Collections;
    
     public class playercontroller : MonoBehaviour
     {
    public float speed;
    
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    
    rigidbody.AddForce(movement);
    }
     }
    

    But you should ask questions yoy may have about Unity3D here: http://forum.unity3d.com/

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Carl Cai Tuesday, November 25, 2014 12:04 PM
    Sunday, November 16, 2014 4:05 PM

All replies

  • Perhaps you should use an operator, maybe rigidbody.AddForce(movement * speed * Time.deltatime).

    Sunday, November 16, 2014 3:37 PM
  • The rigidbody.AddForce method takes three float parameters and an optional ForceMode or a Vector3 and an optional ForceMode: http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    Since your speed field is never used, I assume you want to pass the Vector3 to the AddForce method:

    using UnityEngine;
     using System.Collections;
    
     public class playercontroller : MonoBehaviour
     {
    public float speed;
    
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    
    rigidbody.AddForce(movement);
    }
     }
    

    But you should ask questions yoy may have about Unity3D here: http://forum.unity3d.com/

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Carl Cai Tuesday, November 25, 2014 12:04 PM
    Sunday, November 16, 2014 4:05 PM