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 Sangar · Mar 22, 2014 at 06:57 PM · shootingcursorrunninginfinite runner

Moving a character whilst also aiming with a crosshair TPS

Hi guys, thanks for replying. It seems like my message has been cut off half way for some reason. I will repost:

Hi guys! I am currently creating a infinite runner which automatically moves the character forwards and the terrain backwards. I wanted to use the mouse as the cursor to shoot objects on screen, whilst also rotating the character left or right (doesn't have to be exactly where the mouse is pointing, rotation of character should be a -60* to 60*). At the moment I am using a mouse orbit script, but the mouse positioning is awkward, sometimes to the side of the character even when he is running forward (see screenshot attached). Is there a way I am able to have it so that if the cursor/crosshair is exactly in the middle of the screen, the character will run forwards and if left or right (depending on how far out it is) then the character will travel in that direction?

Cursor on top left hand corner

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 var maxZoom : float = 15;
 var minZoom : float = 3;
 //var speedZoom : float = 10;
 
 
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
             
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target) {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
          
          y = ClampAngle(y, yMinLimit, yMaxLimit);
            
         
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
         target.rotation = rotation;
         transform.rotation = rotation;
         transform.position = position;
         distance  = Mathf.Clamp (distance + Input.GetAxis("Mouse ScrollWheel"), minZoom, maxZoom);
             }
 }
 
 static function ClampAngle (angle : float, min : float, max : float) {
     if (angle < -180)
         angle += 180;
     if (angle > 180)
         angle -= 180;
     return Mathf.Clamp (angle, min, max);
 }


screen shot 2014-03-22 at 18.50.36.png (331.8 kB)
Comment
Add comment · Show 4
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 ShadoX · Mar 22, 2014 at 06:57 PM 0
Share

What exactly is the problem? What have you tried? What do you need help with ?

avatar image robertbu · Mar 22, 2014 at 08:07 PM 0
Share

There are a lot of unknown in your question. Imagine you are standing outside point out object for a friend to shoot at. The rotation your friend need to hit the target will be different than your rotation. And the differences in the two rotation will vary based on the distance the target is away from the two of you. You have the same triangulation issue with your crosshair/TPS scenario. The first decision is ai$$anonymous$$g. You can do:

  • Raycast. Only allow the gun to shoot if the raycast hits something.

  • Use a fixed distance. That is, the shooter will hit the middle of the crosshairs at some specified distance.

  • $$anonymous$$ix the two. Use Raycast when it hits something and use distance when the Raycast fails.

The second decision is rotation. Choices:

  • Character's rotation places the crosshair

  • Crosshair is used to rotate the character

  • Character and crosshair are independent and the shooting does not follow the forward of the character.

Note if using Raycasting, the angle the character rotates will can vary greatly with just subtle moves of the mouse (since targets will not be at a fixed distance from the TPS).

avatar image Sangar · Mar 22, 2014 at 09:58 PM 0
Share

@robertbu It seems as if there was an error when posting my original question, as it came through incomplete. Please see the updated version above :)

avatar image Sangar · Mar 22, 2014 at 09:59 PM 0
Share

@ShadoX Please see the question again above, there was an error when posting earlier! Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bdorn14 · Mar 22, 2014 at 07:23 PM

You need to add a animation to make the character 'turn' while running, and add a script in that will make the character 'follow' the mouse cursor. There should be one in the default unity assets package.

Comment
Add comment · 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

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

22 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

Related Questions

Multiple animation at the same time 2 Answers

How : Player move forward automatically (global Z axis) & 'not' the direction it's facing 3 Answers

Set borders to mouse follower? 0 Answers

Why does the cursor not appear in the TExtField 0 Answers

How to make an object point at the cursor 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