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 /
This question was closed Jan 30, 2015 at 09:07 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by pjlx911 · Jan 29, 2015 at 11:46 AM · c#physicsprogrammingplayer movementspaceship

Tightening up turning radius with AddRelativeForce (or other options)

Hi, how is everyone? I tried asking this in the forums, but I guess no one wanted to help there, so I'm asking it here.

I have been working on this game project for a school studio project (and possibly it might end up being more if I could get this one thing working correctly). The idea is to be a arcade space racing game, subsequently, I want to have physics that are quick (like the ability to turn yaw-wise in a quick fashion).

The issue I keep running into is how to pull this off? I have the script working, I have barrel rolls, strafing, but I am using rigidbody.AddRelativeForce as the means of propulsion/movement. This due to the fact that I couldn't find anyway outside of using Unity's physics system for this. I tried transform.Translate, but that would only propel the ship in one direction, along the z-axis, and not the direction the ship is pointing in. So I am also open to figuring out a non-Unity physics-based method to get this working (if anyone knows, otherwise I'll stick to using rigidbody physics) as the physics system seems laggy/lazy to me turning-wise.

So can anyone help me? Here is my script for just the ship's basic controls.

Thanks for any help.

     using UnityEngine;
     using System.Collections;
      
     public class PlayerShip_Easy : MonoBehaviour
     {
             private float shipSpd = 300.0f;
             private float turnSpd = 150.0f;
             private float rotSpd = 50.0f;
             private float strafeSpd = 50.0f;
      
         float rollRate = 0.0f;
         float pitchRate = 0.0f;
         float yawRate = 0.0f;
      
             float invert = -1.0f;
      
     void Update ()
         {
             float roll = Input.GetAxis("Roll");
             float pitch = Input.GetAxis("Pitch");
             float yaw = Input.GetAxis("Yaw");
             float strafe = Input.GetAxis("Strafe");
      
             if (Input.GetAxis("Pitch") > 0)
             {
                 pitchRate -= pitch * invert * turnSpd * Time.deltaTime;
                 pitchRate = Mathf.Clamp(pitchRate, -45.0f, 45.0f);
                 transform.rotation = Quaternion.Euler(pitchRate, yawRate , 0);
             }
             else if (Input.GetAxis("Pitch") < 0)
             {
                 pitchRate += pitch * turnSpd * Time.deltaTime;
                 pitchRate = Mathf.Clamp(pitchRate, -45.0f, 45.0f);
                 transform.rotation = Quaternion.Euler(pitchRate, yawRate , 0);
             }
      
             if (Input.GetAxis("Yaw") > 0)
             {
                 yawRate += yaw * turnSpd  * Time.deltaTime;
                 yawRate = Mathf.Clamp(yawRate, -60.0f, 60.0f);
                 transform.rotation = Quaternion.Euler(pitchRate, yawRate, 0);
             }
             else if (Input.GetAxis("Yaw") < 0)
             {
                 yawRate += yaw * turnSpd  * Time.deltaTime;
                 yawRate = Mathf.Clamp(yawRate, -60.0f, 60.0f);
                 transform.rotation = Quaternion.Euler(pitchRate, yawRate, 0);
             }
      
         rigidbody.AddRelativeForce(pitch, yaw, shipSpd);
     }
 
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 sniper43 · Jan 29, 2015 at 12:20 PM 0
Share

Have you tried using transform.forward to in a funciton like the following?

 transform.position += transform.forward; // lerp it for smoother movement.


transform.forward is the vector the ship is curently facing and always has a length of 1.

The same logic applies to transform.up, transform.down, transform.right, transform.left and transform.back.

Read more about it here:

http://docs.unity3d.com/ScriptReference/Transform.html

Specifically transform.forward:

http://docs.unity3d.com/ScriptReference/Transform-forward.html

avatar image meat5000 ♦ · Jan 29, 2015 at 12:34 PM 0
Share

AddTorque? Or AddRelativeTorque?

You can also manipulate the Velocity directly.

Never use transform.position or Translate with a rigidbody unless you are trying to teleport and negate the Physics system.

avatar image sniper43 · Jan 29, 2015 at 01:10 PM 0
Share

But I put the transform in because he doesn't really use rigidbodies at all, excpet for the last line. So I posted that thinking it'd do for a solution.

Here's the code for moving Rigidbodies forward:

  rigidB.AddForce( transform.forward * multiplierFloat );

@Baste put together a better answer below anyway.

avatar image meat5000 ♦ · Jan 29, 2015 at 01:11 PM 0
Share

It'd work. Don't get me wrong.

But in the question he states a desire for a Physics based solution :)

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Baste · Jan 29, 2015 at 12:55 PM

If you want more direct control over how the ship moves, setting the velocity vector instead of adding force would work better.

That will make the movement of whatever you're moving less "realistic", but unless you're making a simulator of some sort, you usually get results that feel better (more responsive controls, etc.) if you set the velocity directly.

So I'd try to continue turning the ship like you're doing, but replacing the rigidbody.AddRelativeForce with:

 rigidbody.velocity = transform.forward * shipSpd;

That will give you a very tight turn radius. In fact, you'll always be moving directly forward. If that's too arcadey, and you want to keep some of the speed in the last direction you were moving, just Lerp between your current velocity and that velocity:

 Vector3 forward = transform.forward * shipSpd;
 float turnFactor = .1f;
 rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, forward, turnFactor * Time.deltaTime);

changing the turnFactor value will change your turn radius; the higher the turnFactor, the tighter the turn. Hope that helps!

Comment
Add comment · Show 5 · 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 sniper43 · Jan 29, 2015 at 02:07 PM 1
Share

Wait...

  float forward = transform.forward * shipSpd;


Shouldn't that be

  Vector3 forward = transform.forward * shipSpd;
avatar image Baste · Jan 29, 2015 at 07:02 PM 0
Share

Deeerp, you're right. Fixed it!

avatar image meat5000 ♦ · Jan 30, 2015 at 02:36 PM 0
Share

@Baste Hit that thumbs up button dude :P

avatar image pjlx911 · Jan 30, 2015 at 05:18 PM 0
Share

Hi, thanks for the response. Just give me a sec to try out the idea. :P

avatar image pjlx911 · Jan 30, 2015 at 07:42 PM 0
Share

BRILLIANT!!

Thanks for the help, it worked like a charm!

I would give a thumbs-up, but the system is saying that I can't (lack 15 rep points :'( .... )

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

Multiple Cars not working 1 Answer

How would I get collision at different speeds? 2 Answers

Physics.OverLapSphere is not working Please help me to fix it or replace it 0 Answers

Distribute terrain in zones 3 Answers

Similar movement to a game (look description) 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