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

Boat Script

Discussion in 'Scripting' started by Darkdragon0011, Jun 3, 2016.

  1. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Hi, I found a working boat script bi when i move left or right it spins round and round and does not stop does anyone know how to solve this problem??

    Thanks, (Your friendly Noob)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class boat : MonoBehaviour {
    5.  
    6.     public float turnSpeed = 1000f;
    7.     public float accellerateSpeed = 1000f;
    8.  
    9.     private Rigidbody rbody;
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         rbody = GetComponent<Rigidbody>();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         float h = Input.GetAxis("Horizontal");
    21.         float v = Input.GetAxis("Vertical");
    22.  
    23.         rbody.AddTorque(0f,h*turnSpeed*Time.deltaTime,0f);
    24.         rbody.AddForce(transform.forward*v*accellerateSpeed*Time.deltaTime);
    25.  
    26.  
    27.     }
    28. }
    29.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I would imagine the original usage of the script depended on some friction between the water and the ship?
     
  3. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    I think so
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Try increasing the angular drag on the Rigidbody.
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi
    Here is a altenative boat script that is part of the unity community ocean project you can find additional files at the github page if you need .including a couple boat models
    https://github.com/eliasts/Ocean_Community_Next_Gen




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class BoatController : MonoBehaviour{
    6.  
    7.     [SerializeField] private List<GameObject> m_motors;
    8.  
    9.     [SerializeField] private bool m_enableAudio = true;
    10.     [SerializeField] private AudioSource m_boatAudioSource;
    11.     [SerializeField] private float m_boatAudioMinPitch = 0.4F;
    12.     [SerializeField] private float m_boatAudioMaxPitch = 1.2F;
    13.  
    14.     [SerializeField] public float m_FinalSpeed = 100F;
    15.     [SerializeField] public float m_InertiaFactor = 0.005F;
    16.     [SerializeField] public float m_turningFactor = 2.0F;
    17.     [SerializeField] public float m_accelerationTorqueFactor = 35F;
    18.     [SerializeField] public float m_turningTorqueFactor = 35F;
    19.  
    20.     private float m_verticalInput = 0F;
    21.     private float m_horizontalInput = 0F;
    22.     private Rigidbody m_rigidbody;
    23.     private Vector2 m_androidInputInit;
    24.  
    25.     private float accel=0;
    26.     private float accelBreak;
    27.  
    28.  
    29.  
    30.      void Start()  {
    31.        // base.Start();
    32.  
    33.         m_rigidbody = GetComponent<Rigidbody>();
    34.        // m_rigidbody.drag = 1;
    35.       //  m_rigidbody.angularDrag = 1;
    36.       accelBreak = m_FinalSpeed*0.3f;
    37.  
    38.         initPosition ();
    39.  
    40.  
    41.     }
    42.  
    43.     public void initPosition()    {
    44.         #if UNITY_ANDROID && !UNITY_EDITOR
    45.         m_androidInputInit.x = Input.acceleration.y;
    46.         m_androidInputInit.y = Input.acceleration.x;
    47.         #endif
    48.     }
    49.  
    50.  
    51.     void Update()    {
    52.         #if UNITY_ANDROID && !UNITY_EDITOR
    53.         Vector2 touchInput = Vector2.zero;
    54.         touchInput.x =  -(Input.acceleration.y - m_androidInputInit.y);
    55.         touchInput.y =  Input.acceleration.x - m_androidInputInit.x;
    56.  
    57.         if (touchInput.sqrMagnitude > 1)
    58.             touchInput.Normalize();
    59.  
    60.         setInputs (touchInput.x, touchInput.y);
    61.         #else
    62.         setInputs (Input.GetAxisRaw("Vertical"), Input.GetAxisRaw("Horizontal"));
    63.         #endif
    64.     }
    65.  
    66.     public void setInputs(float iVerticalInput, float iHorizontalInput)    {
    67.         m_verticalInput = iVerticalInput;
    68.         m_horizontalInput = iHorizontalInput;
    69.     }
    70.  
    71.      void FixedUpdate()    {
    72.         //base.FixedUpdate();
    73.  
    74.         if(m_verticalInput>0) {
    75.             if(accel<m_FinalSpeed) { accel+=(m_FinalSpeed * m_InertiaFactor); accel*=m_verticalInput;}
    76.         } else if(m_verticalInput==0) {
    77.             if(accel>0) { accel-=m_FinalSpeed * m_InertiaFactor; }
    78.             if(accel<0) { accel+=m_FinalSpeed * m_InertiaFactor; }
    79.         }else if(m_verticalInput<0){
    80.             if(accel>-accelBreak) { accel-=m_FinalSpeed * m_InertiaFactor*2;  }
    81.         }
    82.      
    83.         m_rigidbody.AddRelativeForce(Vector3.forward  * accel);
    84.  
    85.         m_rigidbody.AddRelativeTorque(
    86.             m_verticalInput * -m_accelerationTorqueFactor,
    87.             m_horizontalInput * m_turningFactor,
    88.             m_horizontalInput * -m_turningTorqueFactor
    89.         );
    90.  
    91.         if(m_motors.Count > 0) {
    92.  
    93.             float motorRotationAngle = 0F;
    94.             float motorMaxRotationAngle = 70;
    95.  
    96.             motorRotationAngle = - m_horizontalInput * motorMaxRotationAngle;
    97.  
    98.             for(int i=0; i<m_motors.Count; i++) {
    99.                 float currentAngleY = m_motors[i].transform.localEulerAngles.y;
    100.                 if (currentAngleY > 180.0f)
    101.                     currentAngleY -= 360.0f;
    102.  
    103.                 float localEulerAngleY = Lerp(currentAngleY, motorRotationAngle, Time.deltaTime * 10);
    104.                 m_motors[i].transform.localEulerAngles = new Vector3(
    105.                     m_motors[i].transform.localEulerAngles.x,
    106.                     localEulerAngleY,
    107.                     m_motors[i].transform.localEulerAngles.z
    108.                 );
    109.             }
    110.         }
    111.      
    112.         if (m_enableAudio && m_boatAudioSource != null)
    113.         {
    114.          
    115.             float pitchLevel =  m_boatAudioMaxPitch*Mathf.Abs(m_verticalInput);
    116.             if(m_verticalInput<0) pitchLevel*=0.7f;
    117.  
    118.             if (pitchLevel < m_boatAudioMinPitch) pitchLevel = m_boatAudioMinPitch;
    119.  
    120.  
    121.             float smoothPitchLevel = Lerp(m_boatAudioSource.pitch, pitchLevel, Time.deltaTime*0.5f);
    122.  
    123.             m_boatAudioSource.pitch = smoothPitchLevel;
    124.         }
    125.     }
    126.  
    127.     static float Lerp (float from, float to, float value) {
    128.         if (value < 0.0f) return from;
    129.         else if (value > 1.0f) return to;
    130.         return (to - from) * value + from;
    131.     }