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);
}
}
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);
}
}
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)
@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);
}