- Home /
How do you Change Direction of a Shot towards Mouse Position? (Top Down Shooter)
Hi
Making a top down space like shooter, got the player moving how I like it and I've set the mouse up so that the game can see it and everything. Got my shot set up and now I just want to fire it. I would like to be able to get this shot to fire towards the curser is possible.
I've managed to get the shot to instantiate at the shotSpawn location but i'm unsure on how to change its direction? In the code, mousePos stores where my curser is, I would have though I could just put that into a parameter of either the instantiate code or the mover code but apparently not the way I did it. Could anyone offer any suggestions please?
Cheer :)
PlayerController is the script attached to my Player Game Object.
using UnityEngine;
using System.Collections;
ublic class PlayerController : MonoBehaviour
{
//handling
public float rotationSpeed = 450;
public float speed = 20;
public float fireRate;
public Transform input;
private float nextFire;
//system
private Quaternion targetRotation; //stores direction
public PlayerBoundary boundary;
//components
private CharacterController controller;
private Camera cam;
public GameObject shot;
public Transform shotSpawn;
void Start()
{
controller = GetComponent<CharacterController> ();
cam = Camera.main;
}
void Update()
{
if (Input.GetButton ("Shoot") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate()
{
ControlMouse ();
//ControlWASD();
}
void ControlMouse()
{
//set up direction in input variabe
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) //fingers taken off controls, player doesnt reset to defaul position
{
targetRotation = Quaternion.LookRotation(input); //rotates player in the way of the direction
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
}
//Controls Movement
Vector3 motion = input;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs (input.z) == 1) ? .7f : 1; //moving diagonally doesnt travel at twice the speed
controller.Move (motion * speed * Time.deltaTime);
//Sets up mouse position
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
}
Mover has been set up on the Shot itself.
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed;
void Start ()
{
rigidbody.velocity = transform.forward * speed;
}
}
Answer by robertbu · Jul 17, 2014 at 05:12 PM
Inset at line 87:
shotSpawn.LookAt(mousePos);
Note that there is no reason for ControlMouse() to be called from FixedUpdate(). Call it as the first line in Update() (before the shot firing code).
Wizard! Thanks for that and for responding so quickly. :)
I've broke it somehow. Hell knows what I've done. I got it all working great and now I'm getting the error 'NullReferenceException: Object reference not set to an instance of an object' on line 86.
I don't know what to do, I didn't do anything to brake it. :( I can't move my character and can't aim.
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
shotSpawn.LookAt(mousePos);
Your answer
![](https://koobas.hobune.stream/wayback/20220613154330im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How Do I Instantiate An Object Behind My Character 1 Answer
Instantiate for few seconds 1 Answer
touch to instantiate and move object ? 1 Answer
Player instantiates Backwords 0 Answers