- Home /
Question by
antaryelkawar · Sep 27, 2016 at 06:26 AM ·
2drotationprojectile
projectiles not correctly aiming in Unity2D
I have a ship game object which has a child turret prefab. I also have a projectile prefab which is fired when I click on the space. The turret rotates towards the mouse pointer and follows it. but when I click, while the projectile is being fired, it is slightly off in rotation, It misses the target by a recognizable margin.
using UnityEngine;
using System.Collections;
public class TargettingController : MonoBehaviour
{
public Rigidbody2D laser;
public float speed = 10f;
void Update()
{
Vector3 upAxis = new Vector3(0, 0, -1);
Vector3 mouseScreenPosition = Input.mousePosition;
mouseScreenPosition.z = transform.position.z;
Vector3 mouseWorldSpace = Camera.main.ScreenToWorldPoint(mouseScreenPosition);
transform.LookAt(mouseWorldSpace, upAxis);
transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z - 90);
if (Input.GetMouseButtonDown(0))
{
FireLaser();
}
}
void FireLaser()
{
Rigidbody2D laserClone = (Rigidbody2D)Instantiate(laser, transform.position, transform.rotation);
laserClone.AddForce(transform.right * speed * Time.deltaTime * 100);
}
}
I suspect it has something to do with the parent transform. I am just a noob in this and have got just three days of experience. Please guide me through this.
Comment
Your answer
Follow this Question
Related Questions
(2D) Face Object in Direction of Travel 1 Answer
Projetil rotation in straight line trajectory 2 Answers
Angle to Rotation 2 Answers
Side scroller - rotate object towards player. 1 Answer
Start Coroutine after other has finished 4 Answers