Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

space vessel //CarController1.js

Discussion in 'Scripting' started by TheRexenor, Oct 8, 2015.

  1. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi guys, I'm new to Unity and not a programmer!

    I just found a JS script posted by jeffmorris and thought to give it a try with my spaceship project. I attach the script to my space vessel but am getting an error "Input Axis throttle is not setup" The console prompts me to Edit - Project Settings - Input, but I don't know how to use the Input Manager...Is it a quick fix?

    I believe that script is what I am looking for so I

    Thanks!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  4. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi guys! thank you for responding so fast! I think I'm getting there...now it gives me another error however,

    "Array index is out of range" at string "109" Does anyone understand what that means?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm guessing you having dragged the wheels into the scripts "wheels" slot in the inspector, there has to be all 4 of them in that array for that code to work (hardcoded array access without checking the array's size... :()

    109 is the line number in your script that is going wrong... index out of range means you asked for an array member outside the size of the array. I.e. give me the 3rd member when there is only one element in the array.
     
  6. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Well my space vessel doesn't have wheels... :(

    Can this script still work?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    given that that script is mostly about dealing with wheelcolliders, if you don't have any wheel... probably not.

    you've not really explained what it is you are trying to do, just asked for explanations as to what problems your implementation of the script is having, so its hard to say really.
     
  8. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Well, using the code below (by Grim Grin Gaming Character Controller tutorial) I am able to get half of the job done!!!

    Now I need two things:

    1) To be able to control and adjust gravity (with a slider just like MoveSpeed; and RotationSpeed)

    2) To make the vessel stick to the track when there is road below it (I read this can be achieved with Raycast but its too advanced for me...)

    Is it possible for someone to modify the code below so that it incorporates the control and adjustment of gravity for my vessel?

    I attached an image to illustrate my situation.
    Many thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5.  
    6. public class SpaceNavigation : MonoBehaviour
    7.    
    8. {
    9.     public float MoveSpeed;
    10.     public float RotationSpeed;
    11.     CharacterController cc;
    12.    
    13.     // Use this for initialization
    14.     void Start ()
    15.        
    16.     {
    17.         cc = GetComponent<CharacterController>();
    18.     }
    19.     // Update is called once per frame
    20.     void Update () {
    21.        
    22.         Vector3 foward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * MoveSpeed;
    23.         transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") * RotationSpeed * Time.deltaTime,0));
    24.         cc.Move(foward * Time.deltaTime);
    25.         cc.SimpleMove(Physics.gravity);
     

    Attached Files:

  9. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    I updated my code based on this tutorial (#3.3 Jump and Gravity - Unity C# Basic Code)!

    However gravity doesn't seem to affect the vessel :(

    Can anyone point out the mistake on my code?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5.  
    6. public class SpaceNavigation : MonoBehaviour
    7.    
    8. {
    9.     public float MoveSpeed;
    10.     public float RotationSpeed;
    11.     public float gravity = 9.8f;
    12.  
    13.     CharacterController cc;
    14.     Vector3 currentMovement;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.        
    19.     {
    20.         cc = GetComponent<CharacterController>();
    21.     }
    22.     // Update is called once per frame
    23.     void Update () {
    24.        
    25.         Vector3 foward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * MoveSpeed;
    26.         transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") * RotationSpeed * Time.deltaTime,0));
    27.         cc.Move(foward * Time.deltaTime);
    28.        
    29.  
    30.         currentMovement = new Vector3(0, currentMovement.y, Input.GetAxis("Vertical") * MoveSpeed);
    31.         currentMovement = transform.rotation * currentMovement;
    32.  
    33.  
    34.         if (!cc.isGrounded)
    35.             currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
    36.  
    37.         //cc.SimpleMove(Physics.gravity);
    38.  
    39.     }
    40. }