- Home /
How am I able to move my (Player) character in the opposite direction of where I am shooting?
I want my player to move by shooting. Meaning that you get knocked back towards the opposite direction of where you're aiming and shooting. I am using this script to aim and shoot:
public GameObject projectile;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;
private void Update()
{
SetRotation();
if (timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
private void SetRotation()
{
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
Answer by SoulQuick · Apr 17, 2019 at 01:28 PM
So I managed to get it working through the following line: "rb.AddForce((rb.gameObject.transform.position - shotPoint.position) * thrust);"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
//public float offset;
public GameObject projectile;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;
public Rigidbody2D rb;
public float thrust;
private void Update()
{
SetRotation();
if (timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
Debug.Log("Rigid Body position: " + rb.position);
//rb.AddForce(-transform.right * thrust, ForceMode2D.Impulse);
rb.AddForce((rb.gameObject.transform.position - shotPoint.position) * thrust);
Debug.Log("Rigid Body position: " + rb.position);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
private void SetRotation()
{
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
Answer by Tecnophobe · Apr 09, 2019 at 06:25 PM
You can simply take the negative of transform.forward
. You'll need to attach a rigidbody to your Object of course. Then you use AddForce with - transform.forward
as the first parameter and Impulse mode for the second. Should look something like...
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
rb.AddForce(-transform.forward * thrust, ForceMode.Impulse);
timeBtwShots = startTimeBtwShots;
}
rb = your rigidbody
thrust = a float number representing how hard you want the bullet to push (increase this number to push harder)
I do have to add to this that I am not experienced in program$$anonymous$$g. So I tried this, but it is not working... Probably doing something wrong here ^^,
I'd be happy to help you figure it out. Can you share your updated file? It would help to have a screenshot of the values assigned to RB and thrust.
Answer by SoulQuick · Apr 16, 2019 at 11:48 AM
@Tecnophobe Sorry for my late answer, but I basically put it where you put it. I even tried making the Rigidbody2D private and calling it in Start by saying "rb = GetComponent();
public GameObject projectile;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;
public Rigidbody2D rb;
public float thrust;
private void Update()
{
SetRotation();
if (timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
rb.AddForce(-transform.forward * thrust, ForceMode.Impulse);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
private void SetRotation()
{
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
Ah, I see a problem. I thought your game was 3D. For a 2D game, you'll want to use transform.right
. Try with that.
It doesn't seem to work. Hm. Yea I kinda left out it's a 2D game. And also the Force$$anonymous$$ode had to be Force$$anonymous$$ode2D I believe.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195124im_/https://answers.unity.com/themes/thub/images/avi.jpg)