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. Dismiss Notice

Boat Won't Stop Moving

Discussion in 'Scripting' started by Nubz, Dec 2, 2014.

  1. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Today I found this tutorial on getting on a boat and sort of driving it around.
    Works exactly like the one in the video did and I don't get any errors.
    What I am wondering how to make it work better as in it just keeps moving and doesn't really stop to backup and turning is very slow. Also goes right through the terrain but I think that could be solved with some colliders maybe.(correct me if I'm wrong of course but this part isn't my main concern)

    This is the video I got the script from
    .
    And Here is the script any help in the right direction to help me figure this out would be great.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoatControl : MonoBehaviour
    5. {
    6.  
    7.  
    8.     CharacterController myController;
    9.     CharacterMotor cMotor;
    10.     GameObject player;
    11.     Transform myTransform;
    12.  
    13.     float startY;
    14.     bool isDriving = false;
    15.  
    16.     void Start ()
    17.     {
    18.         myController = GameObject.FindObjectOfType<CharacterController>();
    19.         cMotor = GameObject.FindObjectOfType<CharacterMotor>();
    20.         player = cMotor.gameObject;
    21.         myTransform = player.transform.parent;
    22.         startY = gameObject.transform.position.y;
    23.     }
    24.  
    25.     bool IsPlayerCloseToBoat()
    26.     {
    27.         return Vector3.Distance(gameObject.transform.position, player.transform.position)<1;
    28.     }
    29.  
    30.     void SetDriving(bool isDriving)
    31.     {
    32.         this.isDriving = isDriving;
    33.  
    34.         cMotor.enabled = !isDriving;
    35.         myController.enabled = !isDriving;
    36.  
    37.         if (isDriving)
    38.             player.transform.parent = gameObject.transform;
    39.         else
    40.             player.transform.parent = myTransform;
    41.     }
    42.  
    43.     void Update ()
    44.     {
    45.         if (Input.GetKeyDown(KeyCode.E) && IsPlayerCloseToBoat())
    46.             SetDriving(!isDriving);
    47.  
    48.         if (isDriving)
    49.         {
    50.             float forwardThrust = 0;
    51.             if (Input.GetKey(KeyCode.W))
    52.                 forwardThrust = 3;
    53.                 if (Input.GetKey(KeyCode.S))
    54.                 forwardThrust = -1;
    55.              
    56.             rigidbody.AddForce(gameObject.transform.forward * forwardThrust);
    57.  
    58.                 float turnThrust = 0;
    59.             if (Input.GetKey(KeyCode.D))
    60.                 turnThrust = 1;
    61.             if (Input.GetKey(KeyCode.A))
    62.                 turnThrust = -1;
    63.  
    64.             rigidbody.AddRelativeTorque(Vector3.up * turnThrust);
    65.         }
    66.  
    67.         rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, 5);
    68.         Vector3 newPosition = gameObject.transform.position;
    69.         newPosition.y = startY + Mathf.Sin(Time.timeSinceLevelLoad * 2) / 8;
    70.         gameObject.transform.position = newPosition;
    71.     }
    72. }
     
  2. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    I guess no one knows. o_O
     
  3. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Over 50 views besides my own and nothing?:(
     
    Last edited: Dec 2, 2014
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You are mixing forces and transform.position changes. This often leads to unwanted effects and could be a part of the issue.
    What kind of drag value are you using for the rigidbody?
     
    GameForLife and Nanako like this.
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    The tutorial video never went into setting the drag or anything on the rigidbody so it's at 0.
    If the code if just horrible tell me and I'll find a different way to do it lol.

    I probably should explain how the player parents to the boat also.
    He had you put an empty gameobject as a parent of the boat and when the player is in the correct spot you hit E and it
    makes the boat a parent of the player.
    Also the rigidbody is on the gameobject not the boat.

    Now that I think about it this seems like an awfully odd way to do things from the start.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    As mentioned by Dantus, combining the setting of the y position (the bobbing motion in line 68-70 above) with the inherent "driving" motion of the rigidbody can lead to weird drifting, and is to be avoided. Try taking out that sine-wave bobbing motion code and see if the drift goes away.

    And yes, you should set some drag too.

    Kurt
     
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    That pretty much worked but overall I don't like the way the whole thing works I think I decided LOL.
    All I wanted was for the boat to take me across the lake to another spot which I can think of a bunch of different ways to handle that now that I have had time to think about it.
    Thank you for the help both of you.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    I just watched this tutorial myself and I think it is great. The guy did a really good job. It is a great way to teach how to get in and out of vehicles, which forms the foundation of a lot of really fun game mechanics: riding mounts, driving cars, etc.

    My only (very minor) suggestion would be to change the "bob up and down" code so that instead of bobbing the main object with the rigidbody on it, it instead bobs the sub-object (what he calls the visible boat) up and down, leaving the root boat object alone y-position-wise.
     
  9. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    I might mess with it again in the near future.
    I had a few other things I wanted to work on yesterday and got frustrated with the boat.

    But regardless I do like that guys tutorials they are pretty simple to understand until he gets into the math stuff (I'm terrible with math and barely scrape by doing this stuff).

    Thanks for the reply Kurt.