- Home /
AddForce problem 2d
So, i 3 hours trying to make a shooting weapon. Its spawn on weapon, but not moving.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class Bulletshoot : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var x = GameObject.Find("weapon").transform.position.x;
var y = GameObject.Find("weapon").transform.position.y;
KeyCode RightKey = KeyCode.Space;
if (Input.GetKey(RightKey))
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("got it");
var spedx = mousePosition.x - transform.position.x;
var spedy = mousePosition.y - transform.position.y;
Console.WriteLine("Ok, bullet remaind");
transform.position = GameObject.Find("weapon").transform.position;
transform.rotation = GameObject.Find("weapon").transform.rotation;
transform.Rotate(Vector3.forward * -90);
GetComponent<Rigidbody2D>().AddForce(Vector3.forward, ForceMode2D.Impulse);
Debug.Log("got it");
}
}
}
Answer by FlaSh-G · Apr 04, 2018 at 12:23 PM
Vector3.forward is the direction "into" the screen. In a 2D game, unless you have rotated your object in unrecommendable ways, moving into that direction won't have any visible effect. Try Vector3.right
instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155855im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my animated object making copies... Can't select or remove through Animator 0 Answers
2D collision matrix does not work in unity 2020 4 Answers
How I can do knockback without OnColisson or OnTrigger 2 Answers
Is there a substitution for GetKey() for a Gamepad controller in the Input Manager? 1 Answer
Need help with a "roll" script (C#) 1 Answer