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

Updating old movement script

Discussion in 'Scripting' started by JPF55, Jul 13, 2017.

  1. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Hello,
    I had a script working back in the old version of Unity but now I'm using Unity 5.5 and I can't figure out how to get my script to work. All this code did was move an object forward at a constant rate.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5.  
    6. public class PlayerMover : MonoBehaviour {
    7.  
    8.     public int speed;
    9.     public int fSpeed;
    10.     public int rSpeed;
    11.     Event onClick;
    12.     float buttonMove = 0;
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         MovePlayer (Input.GetAxis ("Horizontal"));
    17.         MovePlayer (buttonMove);
    18.     }
    19.  
    20.  
    21.     void MovePlayer(float horizontal)
    22.     {
    23.         float rotatePlayer = horizontal;
    24.        
    25.         transform.Rotate(new Vector3(0, 0, rotatePlayer) * rSpeed);
    26.        
    27.         Vector3 movement = new Vector3 (0.0f, 0.0f, fSpeed);
    28.         rigidbody.velocity = movement * speed;
    29.     }
    30.  
    31.     public void StartMove(float horizontal)
    32.     {
    33.         buttonMove = horizontal;
    34.     }
    35. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Is anything calling StartMove()? Put a breakpoint in it or a debug.log in it and see if someone is calling that. Otherwise buttonMove would be zero.
     
  3. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    the part that is not working is the rigidbody movement.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    I suggest you start sticking Debug.Log() calls in at various places to display some of your values, see if they are what you expect. Things like velocity and movement and speed might be a great place to start looking.
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Silly question, but do you have a rigidbody on the player? Are null exceptions being thrown?

    Also, rigidbody is deprecated. I'd encourage you to use GetComponent instead before Unity takes it out altogether.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Good catch @BlackPete ... Pretty sure it doesn't even compile anymore. Maybe OP is just copy/pasting this code, in which case yeah, GetComponent<RigidBody>() is gonna be necessary instead of 'rigidbody'