• 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
Question by Fayelene · Mar 23, 2016 at 11:37 AM · c#rotationvector3motion

How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position?

Hullo commUnity,

I'm a programming student trying to put together a top-down 2d space-fighter game for school / portfolio (think Galak-Z, but with mouse and keyboard instead of sticks). I need the physics to feel real, and so far I've got them working the way I want. I just can't have instant motion canceling allowable.

So, the question is a little deceptive. I have my script doing both of those things, in the manner in which I want them to. The problem I'm having is, when rotating around the mouse, the value changes result in an instant canceling of motion along the previous vector, making the rotation vector capable of stopping all momentum, to include momentum generated by the forward/backward function.

The problem I feel is in the fact that I have to set lateralVelocityVector = new v3... instead of lateralVelocityVector += new v3... The issue with the second is that it results in a spiraling outward motion, instead of a locked circle around the mouse.

Halp plz!

 public float thrust;
 public float lateralThrust;

 public Vector3 forwardVector;
 public Vector3 lateralVector;
 public Vector3 forwardVelocityVector;
 public Vector3 lateralVelocityVector;

 public Vector3 velocityVector;

 public float maxSpeed;
 public float minSpeed;
 
 void Start () {
     forwardVector = new Vector3 (0,0,0);
     lateralVector = new Vector3 (0,0,0);

     velocityVector = new Vector3 (0,0,0);
     maxSpeed = 40;
     minSpeed = -30;

     thrust = 0.1f;

     lateralThrust = 10f;
 }

 void Update(){
     Vector3 position = Camera.main.WorldToScreenPoint(transform.position);
     Vector3 direction = Input.mousePosition - position;
     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis((angle -90), -Vector3.up);
 }

 void FixedUpdate () {

     if (Input.GetKey ("w")){
         /*Get the forward vector while the button is pressed
          * and store it. Set transform vector to forward vector
          * plus lateral vector and move toward it.
          */ 
         forwardVector = (transform.forward * Time.deltaTime) * thrust;

         float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);

         forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
     } else if (Input.GetKey("s")){
         forwardVector = (-transform.forward * Time.deltaTime) * thrust;
         
         float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);
         
         forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
     } else {
         float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);

         forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
     }

     if (Input.GetKey ("a")) {
         lateralVector = (-transform.right * Time.deltaTime) * lateralThrust;

         float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);

         lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
     } else if (Input.GetKey ("d")) {
         lateralVector = (transform.right * Time.deltaTime) * lateralThrust;
         
         float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);
         
         lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
     } else {
         float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
         float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);
         
         lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
     }

     velocityVector = forwardVelocityVector + lateralVelocityVector;

     transform.position += velocityVector;
 }

Sorry about the formatting. First question, and not familiar with posting here ;p

Comment

People who like this

0 Show 2
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 Fayelene · Mar 23, 2016 at 02:04 AM 0
Share

So, I eventually went with a rigidbody and AddForce to accomplish my goal. Still, I wouldn't mind a vector-based solution, if some super-genius has one up their sleeve. In any case, here's the code for the solution :)

 using UnityEngine;
 using System.Collections;
 
 public class FlightController : MonoBehaviour {
 
     public float thrust;
     public float lateralThrust;
 
     public Rigidbody rb;
 
     void Start () {
         rb = GetComponent<Rigidbody> ();
     }
 
     void Update(){
         Vector3 position = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 direction = Input.mousePosition - position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis((angle -90), -Vector3.up);
     }
 
     void FixedUpdate () {
 
         if (Input.GetKey ("w")){
 
             rb.AddForce(transform.forward * thrust);
 
         } else if (Input.GetKey("s")){
 
             rb.AddForce(-transform.forward * thrust);
 
         }
 
         if (Input.GetKey ("a")) {
 
             rb.AddForce(-transform.right * lateralThrust);
 
         } else if (Input.GetKey ("d")) {
 
             rb.AddForce(transform.right * lateralThrust);
 
         }
     }
 }
 
avatar image meat5000 Fayelene · Mar 23, 2016 at 11:36 AM 0
Share

Rigidbody is the better approach. But for Vector based movement you want to Normalize

https://www.google.co.uk/search?q=unity+normalize&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=T3_yVpaWBoWv7Qbq05W4BA&gws_rd=ssl

This should avoid outward spiralling (provided you dont use += which will just keep adding to the value)

Your vector based method would not properly handle collisions.

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody: Linear motion to Circular motion 0 Answers

Using quaternions to align game objects. 1 Answer

How can I bind this script into the state of my camera? 2 Answers

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

Rotate object in 3d space along one axis in the direction of movement vector. 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