Hi,
while waiting for my question getting to be published, I think I got what I want:
I tried to throw a GameObject like in the old zelda games (let Player grab something and trhow it to the left or right, up or down).
I tried with AddForce, but that didn’t do it, it had no curve.
Now I just set the rigidbody2d’s Position to a specific Vector2(3f,6f) to get it a little higher while moving on the x axis and let gravity handle the fall.
Falling will be stopped, when the object reaches the y Position of the Player (set kinematic to true, gravity to 0).
My question now is: could this be handled in a better way? Because in other threads some said, that you should not access velocity or position of a rigidbody2d directly, instead use AddForce.
Best regards,
Christopher
EDIT:
Ok, so here is the code:
First the Player class(only the throw relevant part):
void FixedUpdate () {
//Debug.Log ("Height: " + Screen.height + " Width: " + Screen.width);
//Debug.Log ("Bounds Height: " + height + " Bounds Width: " + width);
Resolution[] res = Screen.resolutions;
//Debug.Log ("Screen resolution: " + Screen.currentResolution.width);
Movement ();
EnforceBounds ();
}
void Movement(){
anim.SetBool ("walk", false);
if (Input.GetKey (KeyCode.D)) {
anim.SetBool ("walk", true);
transform.Translate(Vector2.right *3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0,0);
//Disable diagonal walking
//return;
}
if (Input.GetKey (KeyCode.A)) {
anim.SetBool ("walk", true);
transform.Translate(Vector2.right *3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0,180);
//transform.GetChild(0).transform.eulerAngles = new Vector2(0,180);
//return;
}
if (Input.GetKey (KeyCode.W)) {
anim.SetBool ("walk", true);
transform.Translate(Vector2.up *3f * Time.deltaTime);
//return;
}
if (Input.GetKey (KeyCode.S)) {
anim.SetBool ("walk", true);
transform.Translate(-Vector2.up *3f * Time.deltaTime);
//return;
}
if (Input.touchCount> 0) {
anim.SetBool ("walk", true);
transform.Translate(-Vector2.up *3f * Time.deltaTime);
return;
}
if (Input.GetKey (KeyCode.E)) {
if(PickUp.pickUpObject && !PickUp.picked){
PickUp.picked = true;
//PickUp.throwItem = false;
}
}
if (Input.GetKey (KeyCode.R)) {
if(PickUp.pickUpObject && PickUp.picked){
PickUp.picked = false;
PickUp.throwItem = true;
}
}
}
Now the PickUp Script (which includes the throwing logic):
public class PickUp : MonoBehaviour {
public static bool pickUpObject = true;
public static bool picked = false;
public static bool throwItem = false;
GameObject player;
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
}
// Update is called once per frame
void Update () {
if (picked) {
collider2D.isTrigger = true;
transform.parent = player.transform;
Vector3 pos = GameObject.Find("startCurve").transform.position;
transform.position = pos;
}
}
void FixedUpdate(){
if (throwItem) {
StartCoroutine ("ThrowItem");
}
}
IEnumerator ThrowItem(){
rigidbody2D.gravityScale = 1f;
rigidbody2D.isKinematic = false;
float playLowerY = player.transform.position.y - player.renderer.bounds.size.y/2+renderer.bounds.size.y/2;
// Vector3 targetVec = transform.position + new Vector3(3f,6f,0f);
// Vector3 force2 = (rigidbody2D.position-targetVec).normalized*20;
// Debug.Log ("forceX: " + force2.x + " forceY: " + force2.y);
transform.parent = null;
At this point I first tried with AddForce instead of rigidbody2D.position, but that didn’t get me a curve
Vector2 force = new Vector2(3f,6f);
rigidbody2D.position += force*Time.fixedDeltaTime;
if(transform.position.y < playLowerY){
rigidbody2D.isKinematic = true;
rigidbody2D.gravityScale = 0f;
throwItem = false;
collider2D.isTrigger = false;
}
yield return null;
}
/* void OnTriggerEnter2D(Collider2D other){
pickUpObject = true;
}
void OnTriggerExit2D(Collider2D other){
pickUpObject = false;
}
*/
}
Hope it becomes clear now.