• 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
1
Question by Unityyh · Sep 14, 2014 at 05:47 PM · c#movementmouse

How to make my spaceship move by mouse?

I am trying to make a game very similar to space shooters project. I am adding a few tweaks but the main problem I am having is that I cannot write a valid c# script for the spaceship movement. The game is in 2d, but I am using 3d models and 3d physics. I want to make spaceship move according to mouse position. I am kinda lost in scripting reference and I do not know what to use. Any suggestions, hints or examples would be very apreciated!

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 VesuvianPrime · Sep 14, 2014 at 07:06 PM 0
Share

What code have you written so far? How do you want the mouse to effect the ship?

2 Replies

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

Answer by bilo-lwabona · Sep 14, 2014 at 07:18 PM

As you said, despite the 3d environment/content you can use 2D logic. With your axes of choice, e.g. x & y axes for horizontal and vertical movement (neglecting the z-axis) you could write a mapping between the mouse motion and the ship's motion. What you need is the following

  • Vector2 deltaMousePosition ... this will be adjusted in the update loop with Unity's Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y")

  • In your update loop add this deltaMousePosition to the ship's transform.position, but make sure to clamp it so it does not go off screen ( a simple if statements should suffice)

That is the basic solution. Hope this helps, otherwise please be more specific with your question.

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
avatar image
0

Answer by dreg_master · Jan 09, 2019 at 04:48 AM

 // Update is called once per frame
 private void Update()
 {
     if (enableThis) Move();
 }

 private void Move()
 {
     //var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
     var deltaX = Input.GetAxis("Mouse X");
     var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);

     transform.position = new Vector3(newXPos, transform.position.y, transform.position.z);
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Distribute terrain in zones 3 Answers

How to make the turret of a tank in a 3rd person game look at the mouse cursor? 1 Answer

How do I make my character lean to the sides? 0 Answers

WSAD controls work fine for W and S, but randomly get reversed for A and D 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