Question… Please help.

Updated on December 19, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on December 10, 2017

Hello, so I recently got back into coding after a while, and I watched a few of Brackey’s 2D platformer videos. I have a problem though. This might be a rather silly question, I’m very sorry for asking but is there a way where my character can only shoot at certain angles (90 degree) each side instead of having the arm model follow the position of the mouse? How can I achieve this?

  • Liked by
Reply
0 on December 13, 2017

You could clamp the rotation.

  • Liked by
Reply
Cancel
0 on December 19, 2017
using UnityEngine;
 
public class 2DShoot : MonoBehaviour
{
 public GameObject bulletPrefab;
 void Update()
 {
 if(Input.GetMouseButtonDown(0))//Left Mouse Click
 {
 Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Gets the position of the mouse in world space
 Vector2 direction = worldMousePos - transform.position;//Gets the direction of the mouse from the player
 direction.Normalize ();//Normalizes the direction example vector3(0,1,0) Up
 GameObject bullet = Instantiate (bulletPrefab, transform.position ,Quaternion.identity);//Spawns the object at the players position
 bullet.GetComponent<Rigidbody2D> ().velocity = direction * bulletVelocity;//Adds velocity to the Rigidbody2D on the bullet
 }
 }
}
  • Liked by
Reply
Cancel