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 Hordaland · Sep 11, 2012 at 08:04 PM · positioningonmousedrag

Moving around a weapon on screen using OnMouseDrag

Hey dudes. I'm creating a first person game where the player can move around his weapon on the screen. Basically you're just supposed to hold down a button (so far I'm using LMouse but RMouse will be the final button) and move the mouse and the camera will freeze and the weapon will move instead of the camera.

I've gotten the weapon to move and all using a plane with an OnMouseDrag-function but first of all it's moving way too slow (I want it to move at the same speed as the mouse) and secondly it's jumping back and forth 1 frame at a time when I move the character around while holding the mouse button down.

Any tips on how to straighten things up? Is my method using the plane void?

Here's the code: #pragma strict

 var cam : Camera;
 var controller : GameObject;
 var sword : GameObject;
 var swordDrag : boolean;
 
 internal var gameSens : float;
 internal var screenPosition : Vector3;
 
 function Start()
 {
     gameSens = cam.GetComponent(MouseLook).sensitivityY;
     Screen.lockCursor = true;
     screenPosition = cam.WorldToScreenPoint(transform.position);
 }
 
 function OnMouseDown()
 {
     swordDrag = true;
 
     cam.GetComponent(MouseLook).sensitivityY = 0;
     controller.GetComponent(MouseLook).sensitivityX = 0;
 
     
 }
 
 function OnMouseUp()
 {
     swordDrag = false;
     cam.GetComponent(MouseLook).sensitivityY = gameSens;
     controller.GetComponent(MouseLook).sensitivityX = gameSens;
 }
 
 function OnMouseDrag()
 {
     if (swordDrag == true)
     {
     //Read the mouse input axes
     screenPosition.x += Input.GetAxis("Mouse X");
     screenPosition.y += Input.GetAxis("Mouse Y");
 
     //move the object to the world position to not change screen position
     sword.transform.position = cam.ScreenToWorldPoint(screenPosition);
     }
     //else return;
 }
Comment
Add comment · Show 1
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 Hordaland · Sep 12, 2012 at 12:38 PM 0
Share

The "jumping back and forth" (that is the main problem) almost seems to create two swords, one that stays in the right place and flicker every frame and another one that also flickers every frame and kinda moves around based on how I move my character. If I strafe left the sword moves right and vice versa. I don't know if it's because the camera points at a different spot on the plane or what. Can anyone think of a better way to get the result I want? You don't need to give me the entire code or anything, just a pointer in the right direction.

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

VUFORIA MULTI-TARGETS: SCRIPTING & CHILD HOLOGRAM VISUALIZATION 0 Answers

onMouseDrag scroll 0 Answers

GUI changes when game runs full screen 2 Answers

Position 3d objects in 2d space (orthographic cam) 1 Answer

How to make a building automatically positioned on the terrain 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