Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VAN-D00M · Jul 17, 2014 at 05:08 PM · c#instantiatetop down shooter

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;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VAN-D00M · Jul 17, 2014 at 05:37 PM 0
Share

Wizard! Thanks for that and for responding so quickly. :)

avatar image VAN-D00M · Jul 17, 2014 at 07:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges