1
\$\begingroup\$

I'm trying to move a game object within a set x and y range. I thought the code below would work, but it does nothing. The idea is the player controls the platform and tries to move under the ball. Any suggestions would be appreciated.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary{
    public float xMin, xMax, yMin, yMax;
}

public class PlatformController : MonoBehaviour {

    public float speed = 10f;
    public Boundary boundary; 
    public Rigidbody platformRB;  



    void Start(){
        platformRB = GetComponent<Rigidbody> ();
    }

    void FixedUpdate(){

        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0f);
        platformRB.velocity = movement * speed; 

        platformRB.position = new Vector3 (
            Mathf.Clamp (platformRB.position.x, boundary.xMin, boundary.xMax),
            Mathf.Clamp (platformRB.position.y, boundary.yMin, boundary.yMax),
            0f
        );
    } 
}

Here's how the rolling ball works (as is intended)

using UnityEngine;
using System.Collections;

public class BallMovement : MonoBehaviour {

    public Rigidbody ballRB;
    public float thrust;


    // Use this for initialization
    void Start () {
        ballRB = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void FixedUpdate(){
        ballRB.AddForce (Vector3.right * thrust);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Add some Debug.Log messages to ensure your velocity and forces are getting applied. Is thrust 0f? \$\endgroup\$ – Chris McFarland Jan 28 '16 at 2:19
  • \$\begingroup\$ BallMovement works just fine, that was just for reference. PlatformController is where I'm having headaches. \$\endgroup\$ – Tony Chacon Jan 28 '16 at 2:24
  • 2
    \$\begingroup\$ what are your xMin xMax yMin` and yMax values? \$\endgroup\$ – Hamza Hasan Jan 28 '16 at 8:24
  • 3
    \$\begingroup\$ Your Min and Max values are all 0. \$\endgroup\$ – jgallant Jan 28 '16 at 11:14
  • 1
    \$\begingroup\$ Your platform obviously cannot move because your boundary equals 0 \$\endgroup\$ – Savlon Jan 28 '16 at 20:34
0
\$\begingroup\$

There are multiple things I can think of that could be causing you problems. Here are some things to look at:

  • If your rigidbody is set kinematic, setting velocity will have no effect.
  • Add debug statements like those in the code below to see what is or isn't working as intended
  • Try removing the boundary checking statement to eliminate that as a potential cause of problems
  • Movement applied by changes to the velocity property of a rigidbody happens between LateUpdate frames, rather than in the middle of them. So when you set position after applying velocity, the movement
  • Apply movement directly yourself (as detailed in code below) rather than relying on the physics engine to do it. This has the added benefit of ensuring the gameobject doesn't leave the bounding box, as movement caused by rigidbody.velocity is applied between FixedUpdate() iterations. This means that your code as is will have stuttering at the edges.
  • Lastly, is this a 2D game? It sounds like it might be, especially with the X/Y orientation for your movement. If this is the case, you might want to look at using Rigidbody2D components instead of Rigidbody.

If these don't help you figure out the cause, please update with your results and I'll look deeper. Here's my suggested code (assuming a 3D project):

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary{
    public float xMin, xMax, yMin, yMax;
}

public class PlatformController : MonoBehaviour {

    public float speed = 10f;
    public Boundary boundary; 
    public Rigidbody platformRB;  

    void Start(){
        platformRB = GetComponent<Rigidbody> ();
    }

    void FixedUpdate(){

        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Debug.Log("Boundary: (" + boundary.xMin + "," + boundary.xMax + "," + boundary.yMin + "," + boundary.yMax + ")");

        Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0f);

        Debug.Log("Movement: " + movement);

        //platformRB.velocity = movement * speed; 

        // Calculate per-frame movement based on movement, speed, and fixedDeltaTime
        Vector3 move = movement * speed * Time.fixedDeltaTime;
        Debug.Log("Move: " + move);

        /*
        platformRB.position = new Vector3 (
            Mathf.Clamp (platformRB.position.x, boundary.xMin, boundary.xMax),
            Mathf.Clamp (platformRB.position.y, boundary.yMin, boundary.yMax),
            0
        );
        */

        Debug.Log("Pre-move position: " + platformRB.position);

        Vector3 newPosition = new Vector3(
            Mathf.Clamp(platformRB.position.x + move.x, boundary.xMin, boundary.xMax),
            Mathf.Clamp(platformRB.position.y + move.y, boundary.yMin, boundary.yMax),
            0
        );

        Debug.Log("Desired Position: " + newPosition);

        // Using rigidbody.MovePosition instead of setting the position property directly makes the movement respect interpolation settings
        platformRB.MovePosition(newPosition);

        Debug.Log("Post-move Position: " + platformRB.position);

    } 
}

Note: There may be typos in my code that will cause compilation errors - if so let me know

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.