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

Need help with movement, controls, and following

Discussion in 'Editor & General Support' started by candlemaster, Jul 22, 2014.

  1. candlemaster

    candlemaster

    Joined:
    Mar 12, 2014
    Posts:
    16
    So, I'm trying to code the input for a game where the player is able to fly around, like a plane, and is also followed by other objects, like a snake, and I'm encountering a number of issues.

    I've tried 2 different approaches, one with kinematics (transform.translate(transform.forward * speed);), and one with physics (rigidbody.addforce(transform.forward * speed);). For the script attached to the followers, I move forward the same way. The problem is that if I use kinematics, the player does not collide with the scenery, and can pass right through it, but if I use physics then it introduces a significant amount of jittering. For the purpose of it feeling right, I would rather use the physics approach, so the player feels appropriately "heavy".

    Additionally, when I use physics to push the follower objects forward, they collide with their leader and push it forward, causing the player to move forward when they shouldn't be.

    Is there a better way to do this that I'm not aware of?

    Here is the script attached to the front object in the "train":

    Code (csharp):
    1. {
    2.  
    3.     public float speed = 1;
    4.     public float verticalTurnSpeed = 1;
    5.     public float horizontalTurnSpeed = 1;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void FixedUpdate ()
    15.     {
    16.         if (Input.GetMouseButton(1))
    17.         {
    18.            
    19.             rigidbody.AddRelativeForce(new Vector3(0.0f, 0.0f, 1.0f) * speed);
    20.  
    21.             float tiltUp = Input.GetAxis("Mouse Y") * verticalTurnSpeed;
    22.             float tiltSide = Input.GetAxis("Mouse X") * horizontalTurnSpeed;
    23.  
    24.             //transform.Rotate(new Vector3(0.0f, 1.0f, 0.0f), tiltSide);
    25.             //transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f), tiltUp);
    26.  
    27.             rigidbody.AddRelativeTorque(new Vector3(0.0f, 1.0f, 0.0f) * tiltSide);
    28.             rigidbody.AddRelativeTorque(new Vector3(-1.0f, 0.0f, 0.0f) * tiltUp);
    29.         }
    30.     }
    31. }
    And here is the script attached to everything else:

    Code (csharp):
    1. {
    2.         /* The object that this segment is following.  Doesn't have to be a SnakeBody. */
    3.     public GameObject leader;
    4.         /* How close to the leader we should try to get. */
    5.     public float clearance;
    6.         /* The fastest we're allowed to move towards the leader. */
    7.     public float speed;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void FixedUpdate ()
    17.     {
    18.         transform.LookAt(leader.transform.position, new Vector3(0.0f, 1.0f, 0.0f));
    19.  
    20.         Vector3 target = transform.position - leader.transform.position;
    21.         target.Normalize();
    22.         target *= clearance;
    23.         target = leader.transform.position + target;
    24.  
    25.         //transform.position = Vector3.MoveTowards(transform.position, target, speed);
    26.         rigidbody.AddRelativeForce(transform.forward * speed);
    27.     }
    28. }
     
  2. candlemaster

    candlemaster

    Joined:
    Mar 12, 2014
    Posts:
    16