Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How am I able to move my (Player) character in the opposite direction of where I am shooting?

$
0
0
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); } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles