Saturday, November 14, 2015

unity - How to make projectiles shoot at mouse position?


I'm looking to make it so my fired projectile is shot at the position of the mouse so aiming is an option ie:up down ect..


I have the script working to where it spawns the cloned object and launches it but it only shoots straight from my players hand before disappearing. how can I adjust my command so the object is shot in the direction the mouse is pointing?


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileShooter : MonoBehaviour {

[SerializeField]
Transform attachmentPoint;
GameObject prefab;


void Start () {

prefab = Resources.Load ("projectile") as GameObject;
}


// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
GameObject projectile = Instantiate (prefab) as GameObject;
projectile.transform.position = attachmentPoint.position + Camera.main.transform.forward * 2;
Rigidbody rb = projectile.GetComponent ();
rb.velocity = Camera.main.transform.forward * 40;
Destroy (projectile, 2.5f);
}
}

}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...