0
\$\begingroup\$

I am currently trying to launch a gameobject using Rigidbody2d.velocity but whenever I launch it goes backward instead of forwards. I have already tried making the vector 3 that I am using negative, but nothing seems to work. Please help as I don't even know why this happening.

Here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArmPower : MonoBehaviour {

    //Priority Script

    public ArmAngle armAngle;
    public bool allowLaunch = false;
    public GameObject armPowerSlider;
    public bool ifLaunched;
    public GameObject armPowerPhone;
    public StartAllowStrengthPower startAllow;
    public PhoneController armPhoneController;

    // Use this for initialization
    void Start () {
        armAngle = FindObjectOfType<ArmAngle>();
        armPowerSlider = GameObject.Find("PowerSlider");
        ifLaunched = false;
        armPowerPhone = GameObject.Find("Phone");
        startAllow = FindObjectOfType<StartAllowStrengthPower>();
        armPhoneController = FindObjectOfType<PhoneController>();
    }

    // Update is called once per frame
    void Update () {
        if (allowLaunch)
        {
            LaunchPhone( (int) (Mathf.Round(armPowerSlider.GetComponent<Slider>().value) ));
            ifLaunched = false;
        }
        if(startAllow.allowLaunchChanger <= 0)
        {
            LaunchPhone((int)(Mathf.Round(armPowerSlider.GetComponent<Slider>().value)));
        }


    }

    public void LaunchPhone(int launchAmount)
    {


        Vector3 launchValue = new Vector3(armPowerPhone.transform.position.x, launchAmount, armPowerPhone.transform.position.z);

        armPowerPhone.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;

        armPowerPhone.GetComponent<Rigidbody2D>().velocity = launchValue;

        armPhoneController.armAudio.Play();

        armPowerPhone.transform.SetParent(null);
        armAngle.allowStrength = false;
        allowLaunch = false;
        ifLaunched = true;
        startAllow.allowLaunchChanger = 5;

    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It looks like here you are trying to set up a velocity vector:

Vector3 launchValue = new Vector3(armPowerPhone.transform.position.x, launchAmount, armPowerPhone.transform.position.z);

But you are putting in the absolute position of the GameObject for the x and z components of the velocity, which doesn’t make sense in this context, I think. Try just using zeros.

Vector3 launchValue = new Vector3(0, launchAmount, 0);
\$\endgroup\$
1
  • \$\begingroup\$ Ok I'll try that as soon as I can \$\endgroup\$ May 29, 2018 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .