- Home /
How to make 2d shooting towards the mouse but have it continue in staright line afterwards?
Title pretty much says it, tight now my code just has the bullet stop at the mouse position, so how would I make it continue past that? I have to codes for shooting, the controll (where the bullet is instantiated) and the bullet code itself which is what handles everything else right now. Ill paste the bullet code here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour {
private Vector2 target;
public float speed;
public float turnSpeed;
void Start()
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
StartCoroutine(destroyAfterTime());
}
private void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "wall" )
{
Destroy(gameObject);
}
}
IEnumerator destroyAfterTime()
{
yield return new WaitForSeconds(2f);
Destroy(gameObject);
}
}
It is stopping at the mouse position, because you are moving to it, but you should be moving in the direction of the mouse position (where it was when you fired the shot).
In Start()
, ins$$anonymous$$d of storing the mouse position as target
, store the direction (normalized), and then in Update()
use $$anonymous$$oveTowards(transform.position, transform.position + direction * speed, speed * Time.deltaTime)
. To get the direction, do something like this:
direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;
Answer by Kciwsolb · Jun 06, 2018 at 03:27 PM
So, do you just want it to fire in a straight line in the direction of the mouse? If so...
Store this variable:
Vector3 moveDirection;
Do this in Start:
moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
moveDirection.z = 0;
moveDirection.Normalize();
Add this in Update:
void Update()
{
transform.position = transform.position + moveDirection * speed* Time.deltaTime;
}
We first store a Vector3 that will be our direction to the mouse.
We then calculate a direction to the mouse based on our mouse position.
Then we set the z value to zero (since we don't want to move on z since this is 2D).
Then we normalize that Vector3 so it has a magnitude of 1.
Then we update our position being sure to account for speed and frame rate.
That's because Transform.position
is a property, not a variable, so you cannot use +=
operator on it. However, this is easy to fix, you can replace it with transform.position = transform.position + <the rest is the same>;
.
Yeah this^ Thanks! This is what happens when I scribble an answer while I'm eating lunch.