Search Unity

Can't move forwards properly

Discussion in 'Scripting' started by tricks21, Nov 16, 2007.

  1. tricks21

    tricks21

    Joined:
    Oct 29, 2007
    Posts:
    13
    Hi all, I have a problem with the boat I’m trying to make and the way it moves. When using the forward keys the boat moves in the forward view of the world and not of the boat.

    Am I approaching this in the best way or is there a different script that could solve my problem.

    Any help would be appreciated,

    (':D')



    Code (csharp):
    1.  
    2.  
    3. var rowSpeed = 20;
    4. var rowtateSpeed = 30;
    5.  
    6.  
    7. function FixedUpdate () {
    8.    
    9.     //this bit controls forward and backward, this line turns the up and down keys into a value from -1 to 1
    10.     var forwardSpeed = Input.GetAxis ("Vertical");
    11.    
    12.     //this line multiplies that last variable by 20
    13.     fwdSpeed = forwardSpeed*rowSpeed;
    14.    
    15.     //Adds a force to the forward axis (Z) by the variable above
    16.     rigidbody.AddForce(Vector3.forward * fwdSpeed);
    17.     //end forward/backward bit
    18.    
    19.    
    20.     // bit controls the rotation. use left and right keys to control rotation....
    21.  
    22.     var spinSpeed = Input.GetAxis ("Horizontal");
    23.    
    24.     // times the variable to give it higher speed
    25.     spinnerSpeed = spinSpeed*rowtateSpeed;
    26.    
    27.    
    28.     // rotates it round the y axis
    29.     var eulerAngleVelocity = Vector3 (0, spinnerSpeed, 0);
    30.     var deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
    31.     rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
    32.  
    33.    
    34.    
    35. }
    36.  
    37.  
    38.  
     
  2. Lka

    Lka

    Joined:
    Aug 6, 2006
    Posts:
    297
    Instead of
    rigidbody.AddForce(Vector3.forward * fwdSpeed);

    try with
    rigidbody.AddForce(transform.TransformDirection(Vector3.forward )* fwdSpeed);

    or
    rigidbody.AddRelativeForce(Vector3.forward * fwdSpeed);
     
  3. tricks21

    tricks21

    Joined:
    Oct 29, 2007
    Posts:
    13
    Hey, thanks a lot, i'll give them both a try.

    :D