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 TSI25 · Sep 25, 2013 at 12:34 AM · 2dmovementspacespaceshipclicktomove

2d Space Flight Movement Script

I'm running into a lot of problems coding, hopefully some one can give me some pointers on things i need to look out for when i do this, or functions i can use that'll make this easier, but essentially what im trying to do is make a 2d game that works with the physics youd find in space, and that uses click to move.

that means: click &hold to move - clicking sets the destination and the time held is how long you're accelerating for.

releasing means that you will continue to travel along that vector at your final speed forever (or until you hit something)

clicking also should rotate the player's ship so that its looking at the point its trying to approach.

the code im using right now (ripped from a wiki) looks like this :

// Click To Move script // Moves the object towards the mouse position on left mouse click var smooth:int; // Determines how quickly object moves towards position private var targetPosition:Vector3; function Update () { if(Input.GetKey(KeyCode.Mouse0)) { var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0; if (playerPlane.Raycast (ray, hitdist)) { var targetPoint = ray.GetPoint(hitdist); targetPosition = ray.GetPoint(hitdist); var targetRotation = Quaternion.LookRotation(targetPoint - transform.position); transform.rotation = targetRotation; } } transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth); }

The rotation function works beautifully, and it will continue to advance toward the location of the mouse click - but ONLY for as long as the click is held down, if the player releases the mouse click the ship stops instantly. further - the ship seems to start out very quickly (in relation to how far it is from the mouse) and slows down a lot as it approaches the location of the mouse click.

I'm really not very good at programming, any help anyone can give would be awesome. I don't expect people to code this for me but if people can point out ways to make the ship continue moving along a vector toward the mouse click, rather than to the mouse click - or can say how to use an acceleration rather than use distance to the target to determine speed that would be fantastic.

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
3
Best Answer

Answer by robertbu · Sep 25, 2013 at 03:01 AM

If you want realistic space physics, then consider using a Rigidbody and AddForce(). But I'm not sure it is what you want, or if your mechanism of aiming and accelerating will work well if you do want realistic physics. You say the turning works well, but the turning does not reflect real physics. In real physics, when you add force it does not immediately cancel out the existing velocity and head in a new direction. In addition your current code will immediate jump to a new rotation. Here is some code that implements a simplified form of real physics using your turn and force model:

 var rotationSpeed = 1.0;
 var force = 3.0;
 
 private var targetPosition:Vector3;
 private var velocity = Vector3.zero;
  
 function Update () {
     if(Input.GetMouseButton(0))
     {
         var pos = Input.mousePosition;
         pos.z = Camera.main.transform.position.y;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         var targetRotation = Quaternion.LookRotation(pos - transform.position);
         velocity += transform.forward * Time.deltaTime * force;
     }
      transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
     transform.position += velocity * Time.deltaTime;
 }

And here is a modification that turns the direction and velocity immediately like your current model. It also projects the current velocity onto the forward vector, so you can slow down by turning the ship:

 #pragma strict
  
 var rotationSpeed = 1.0;
 var force = 3.0;
 
 private var targetPosition:Vector3;
 private var velocity = Vector3.zero;
  
 function Update () {
     if(Input.GetMouseButton(0))
     {
         var pos = Input.mousePosition;
         pos.z = Camera.main.transform.position.y;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         var targetRotation = Quaternion.LookRotation(pos - transform.position);
         transform.rotation = targetRotation;
         velocity = Vector3.Project(velocity, transform.forward);
         velocity += transform.forward * Time.deltaTime * force;
     }
     transform.position += velocity * Time.deltaTime;
 }
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 TSI25 · Sep 25, 2013 at 01:23 PM 0
Share

Thank you so much, this is exactly the help I was hoping for and I can definitely alter this code for my needs. Id upvote your comment but I don't have the rep for it yet

avatar image SN-Nadeem · Mar 26, 2014 at 01:39 PM 0
Share

i want the script but can u please remove rotation bcuz its an 2d sprite

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

16 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

Related Questions

2d Spaceship smooth mouse movement 0 Answers

Mouse Movement (Tracking) 1 Answer

Moving Up, Down, Left and Right via Wasd and the arrow btns?? in 2d 2 Answers

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

wasd movement rigidbody no bouncing 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