More problems with arrow shooting
Hello, i have a sprite that rotates and that sprite holds another empty sprite that instantiates arrows. So when i click and hold mouse button and move it the upper body sprite rotates, and on mouse release the arrow instantiates and should add force with AddForce function. Now here are the problems: 1. It instantiates the arrow but not on the the empty sprites and the rotation misses 10 degrees. 2. After instantiating it should AddForce but i need to click and release and than it will add force but than another arrow is created that is standing still. So the when arrow is created it stands still and i need to click twice in order for AddForce to active. 3. And i someone can show me how to get a arrow like flying path because once i add gravity it goes straight but falls pointing in the same direction. Well you know how arrows fly. It forms a rainbow like path. Thats what im trying to get. Here is my code for Upper body
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Public Vars
[SerializeField]
public Rigidbody2D arrowpoint;
[SerializeField]
public Camera camera;
[SerializeField]
public Rigidbody2D arrow;
public bool shoot;
public Vector3 mousePosition;
public PlayerController play;
public bool inst;
//Private Vars
private Vector3 direction;
private float distanceFromObject;
private Rigidbody2D rigidbody;
private bool isAiming;
private Animator anim;
public static Vector3 dir;
void Start() {
rigidbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
arrowpoint = GetComponent<Rigidbody2D> ();
shoot = false;
inst = true;
}
void FixedUpdate() {
HandleRotation ();
}
void Update(){
HandleShoot();
}
void HandleRotation(){
if (Input.GetMouseButton(0)){
isAiming = true;
//Grab the current mouse position on the screen
/*mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z + camera.transform.position.z));
//Rotates toward the mouse
rigidbody.transform.eulerAngles = - new Vector3(0,0,Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x))*Mathf.Rad2Deg + 180); */
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
void HandleShoot()
{
if (isAiming) {
anim.SetBool("Aiming", true);
}
if (Input.GetMouseButtonUp (0)) {
isAiming = false;
anim.SetBool ("Aiming", false);
shoot = true;
if (shoot) {
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Instantiate (arrow, arrowpoint.position, transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward));
ArrowControl.arrowrb.AddForce(dir.normalized * ArrowControl.thrust);
shoot = false;
inst = true;
}
}
}
}
and here is the arrow code
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerController))]
public class ArrowControl : MonoBehaviour {
public static float thrust = 250;
public static Rigidbody2D arrowrb;
// Use this for initializatio
void Start () {
arrowrb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
}
}
Your answer
Follow this Question
Related Questions
How do i rotate an object 90 or -90 degrees upon collision? 3 Answers
Quaternion.FromToRotation to calculate Rotation between two objects 1 Answer
Any simple ways of keeping track of simple rotation? 2 Answers
Does anyone know how to convert this rotation to lerp 1 Answer
Quaternions cancels transform.Rotate 0 Answers