Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by braydon97 · Aug 10, 2016 at 08:42 PM · c#unity 5playershootingaiming

How do I have my character aim where my mouse cursor is?

Here I took a picture to help you understand more of what I want!: http://imgur.com/a/jHqD6

so the red dot down in the entrance area, is where my mouse cursor was and the second dot above the entrance is where it was shooting, I would like the bullets to shoot where the mouse cursor is and if possible have the gun Move with the cursor on the Y axis (up and down).. also instead of having a cursor is there a way I could use a crosshair "image" that I make instead..

Here is the mouse code

      // Turn the player to face the mouse cursor.
      Turning ();
      
  void Turning ()
  {
      //Create a ray from the mouse cursor on screen in the direction of the camera.
      Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
      // Create a RaycastHit variable to store information about what was hit by the ray.
      RaycastHit floorHit;
      // Perform the raycast and if it hits something on the floor layer...
      if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
      {
          // Create a vector from the player to the point on the floor the raycast from the mouse hit.
          Vector3 playerToMouse = floorHit.point - transform.position;
          // Ensure the vector is entirely along the floor plane.
          playerToMouse.y = 0f;
          // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
          Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);
          // Set the player's rotation to this new rotation.
          playerRigidbody.MoveRotation (newRotatation);
      }
 

I think that's about it these are all the things I am having trouble with and can't figure out i'm a noob thanks in advance for any help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by villevli · Aug 10, 2016 at 10:21 PM

Just remove the line: playerToMouse.y = 0f; (Line 16 on the code you posted here)

You can change the cursor using Cursor.SetCursor or you can make a more sophisticated crosshair that moves in the game world.

One way to make it: Hide the cursor with Cursor.visible and create a gameobject with sprite renderer and your crosshair image set to it. Set the objects position to the floorHit.point and rotation based on the normal at the target point. That's easy to get in your current script from floorHit.normal and the rotation can be set e.q. with crosshair.transform.forward = normal or crosshair.transform.rotation = Quaternion.LookRotation (normal).

Comment
Add comment · Show 1 · 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 braydon97 · Aug 11, 2016 at 02:06 PM 0
Share

I figured out the crosshair, but my character follows the cursor correctly but he can't look up at all it starts off slightly ai$$anonymous$$g torwards the ground and you can't aim up past that, also the bullets don't shoot exactly where the cursor is any ideas?

avatar image
0

Answer by Sethhalocat · Aug 10, 2016 at 10:42 PM

I recently did this in one of my games, im more then happy to share with you the script im using and maybe you can use it. I forced one object to always look at another object that follows the mouse around. Its my clever way since i suck at coding.

looks at mouse:

 using UnityEngine;
 using System.Collections;
 
 public class Follow : MonoBehaviour {
 
     void Update() {
         Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 }

spawns object:

 var Shot : GameObject;
 
 function Update(){
     if (Player.arrowAmount >= 0){
         if (MainAnimations.Scared == false){
             if( Input.GetButtonDown("Fire1"))
             shooting();
         }
     }
 }
 
 function shooting(){
     Player.arrowAmount -= 1;
     Instantiate(Shot, transform.position, transform.rotation);
 }
 

My character shoots arrows and has the ability to run out of arrows, I figured id keep that part in if you wanted to add amunition to your game, if not you can always just take it out. The first one btw was C# and the last one was Java script... I hope this helps! :)

Comment
Add comment · Show 1 · 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 RockingGameDeveloper · Nov 21, 2019 at 01:01 AM 0
Share

He said JavaScript, not C#.

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

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Photon Player Mover Failure 1 Answer

Remember position for a simple player controller (Left/Right)? 0 Answers

Finding target position for particle system based shooting 0 Answers

How can I isolate just the character to slow down 0 Answers

Destroy instantiated Projectile after time 1 Answer


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