Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My ship used to move...

Discussion in 'Editor & General Support' started by Brian-Washechek, May 16, 2018.

  1. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    My ship used to move. I'm unsure what I changed to make it quit.
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Boundary
    7. {
    8.     public float xMin, xMax, yMin, yMax ,zMin, zMax;
    9. }
    10.  
    11. public class PlayerController : MonoBehaviour
    12. {
    13.  
    14.     public float speed;
    15.     public Boundary boundary;
    16.     public float tilt;
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    22.  
    23.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    24.         //rigidbody.velocity = movement; This code is not good anymore!
    25.         GetComponent<Rigidbody>().velocity = movement * speed;
    26.  
    27.         GetComponent<Rigidbody>().position = new Vector3
    28.         (
    29.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    30.             0.0f,
    31.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    32.         );
    33.  
    34.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    35.     }
    36. }
    I think that the error is possibly in that code, but it's more likely to be in the editor. If you don't see it there, would you see if it's somewhere in the project itself? I'll be sure to make note of it once it's discovered.
     
    Last edited: May 16, 2018
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    if you disable setting the position, does it move then?
     
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,629
    Also make sure in the inspector that speed is not 0.
     
  4. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    I checked and my speed is 7.

    How do I disable setting the position?
    Code (csharp):
    1.  
    2.  
    3.     private void FixedUpdate()
    4.     {
    5.         float moveHorizontal = Input.GetAxis("Horizontal");
    6.         float moveVertical = Input.GetAxis("Vertical");
    7.  
    8.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    9.         //rigidbody.velocity = movement; This code is not good anymore!
    10.         GetComponent<Rigidbody>().velocity = movement * speed;
    11.  
    12.                GetComponent<Rigidbody>().position = new Vector3
    13.                (
    14.                    Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    15.                    0.0f,
    16.                    Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    17.                );
    18.  
    19.         GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    20.     }
    21. }
    I don't see a working "set position" command there. There is this:
    Code (csharp):
    1.         //rigidbody.velocity = movement;
    but it's old. Does anyone know of a modern working substitute for rigidbody.velocity = movement?
     
    Last edited: May 16, 2018
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add the following before line 25:
    Code (csharp):
    1.  
    2. Debug.Log("movement: " + movement.ToString() + "  speed: " + speed.ToString());
    3.  
    If either are 0 then velocity will be 0 so you won't be moving (any number times 0 is always 0).

    The uncommented code in there should work fine unless speed was 0, or movement was 0,0,0. The commented code just doesn't scale the velocity by the speed variable and uses the old way of addressing the rigidbody component.
     
    Last edited: May 16, 2018
  6. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    Cool, but a need a replacement for
    Code (csharp):
    1. rigidbody.velocity = movement;
    that Unity likes now-a-days.

    Thanks for teaching me the " Debug.Log("Add the text you want to log here"); " trick, though. I'm sure that will be very useful.
     
    Last edited: May 17, 2018
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Were you following a tutorial for an older version of Unity (e.g. 4.X) by any chance? All the GetComponent<Rigidbody>() calls look like something the auto API updater would do. Add a new field of type Rigidbody to the class, and assign it on Start.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, yMin, yMax ,zMin, zMax;
    8. }
    9. public class PlayerController : MonoBehaviour
    10. {
    11.     public float speed;
    12.     public Boundary boundary;
    13.     public float tilt;
    14.     private Rigidbody myRigidbody;
    15.     private void FixedUpdate()
    16.     {
    17.         float moveHorizontal = Input.GetAxis("Horizontal");
    18.         float moveVertical = Input.GetAxis("Vertical");
    19.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    20.         //rigidbody.velocity = movement; This code is not good anymore!
    21.         myRigidbody.velocity = movement * speed;
    22.         myRigidbody.position = new Vector3
    23.         (
    24.             Mathf.Clamp (myRigidbody.position.x, boundary.xMin, boundary.xMax),
    25.             0.0f,
    26.             Mathf.Clamp (myRigidbody.position.z, boundary.zMin, boundary.zMax)
    27.         );
    28.         myRigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, myRigidbody.velocity.x * -tilt);
    29.     }
    30. }
    This way, the dozens of ugly and slow GetComponent<>() calls are gone, and you can use this line:

    Code (CSharp):
    1. //rigidbody.velocity = movement; This code is not good anymore!
    By changing it to:

    Code (CSharp):
    1. myRigidbody.velocity = movement;
     
  8. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    It's now complaining "The name 'myRigidbody' does not exist in the current context ".
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Did you add the field to the class?

    Also I just realized forgot to assign myRigidbody in the Start() function in my posted example.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, yMin, yMax ,zMin, zMax;
    8. }
    9. public class PlayerController : MonoBehaviour
    10. {
    11.     public float speed;
    12.     public Boundary boundary;
    13.     public float tilt;
    14.     private Rigidbody myRigidbody; // <---- Make sure this is here
    15.  
    16.     private void Start()
    17.     {
    18.         myRigidbody = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     private void FixedUpdate()
    22.     {
    23.         float moveHorizontal = Input.GetAxis("Horizontal");
    24.         float moveVertical = Input.GetAxis("Vertical");
    25.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    26.         //rigidbody.velocity = movement; This code is not good anymore!
    27.         myRigidbody.velocity = movement * speed;
    28.         myRigidbody.position = new Vector3
    29.         (
    30.             Mathf.Clamp (myRigidbody.position.x, boundary.xMin, boundary.xMax),
    31.             0.0f,
    32.             Mathf.Clamp (myRigidbody.position.z, boundary.zMin, boundary.zMax)
    33.         );
    34.         myRigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, myRigidbody.velocity.x * -tilt);
    35.     }
    36. }
     
  10. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    I've discovered where my problem was! In the editor the "Is Kinematic" was checked. I got it working by unchecking it. I don't know if I'm messing it up or not by turning it off. But I do know that turning it off solved my problem. What does that do?
     
  11. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
  12. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    Cool. Thank you for the information.