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);
}
}
thrust
0f
? \$\endgroup\$xMin
xMax
yMin` andyMax
values? \$\endgroup\$