- Home /
Accelerating in Turned Direction
I'm creating a simply space navigation game, but I'm having a problem with "turning and burning."
As of right now, my craft accelerates and decelerates, strafes both left and right, elevates and descends, and rotates. I even have a particle effect working on key presses only.
Here's my problem: you cannot rotate the craft to change your trajectory. In other words, say you were facing and heading towards an asteroid and you see a space dock to your left. You rotate the craft left to face and head towards the space dock, but when you hit the accelerator the craft continues to speed towards the asteroid instead of the direction you're facing currently. I've tried so many different options with MoveRotation, transforms, rigidbodies and so forth, but have yet to fix this seemingly simple issue. If you can help me find a simple solution for this issue I, and I'm sure so many others, would be very grateful. Here's my scripting for the movement; Q and E are being used to initiate the rotation:
Thanks, ~Morgan
===================================================================================
using UnityEngine;
using System.Collections;
public class MovementScript : MonoBehaviour
{
public ParticleEmitter boosters;
void Start()
{
rigidbody.rotation = Quaternion.identity;
}
void FixedUpdate()
{
if(Input.GetKey(KeyCode.W))
{
rigidbody.AddForce(Vector3.forward * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.S))
{
rigidbody.AddForce(Vector3.back * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.A))
{
rigidbody.AddForce(Vector3.left * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.D))
{
rigidbody.AddForce(Vector3.right * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.Q))
{
transform.Rotate(Vector3.forward * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.E))
{
transform.Rotate(Vector3.back * 1);
}
if(Input.GetMouseButton(0))
{
rigidbody.AddForce(Vector3.up * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetMouseButton(1))
{
rigidbody.AddForce(Vector3.down * 1);
boosters.particleEmitter.emit = true;
}
}//END FixedUpDate()
}
few things you can do is use some code like `
if(Input.GetAxis("Vertical")){
rigidbody.AddForce(Vector3.forward * Input.GetAxis("Vertical) *1)
}
this will save a bunch of lines of code as it takes vertical as a number from -1 to 1 and then multiplys it
when you hit the forward button you are working with world space so vector3.forward is always the same direction if you are sitting still and rotate then hit forward you should start going sideways
I know what is happening and why which is you are operating in world space not local space this is a problem I have had many times and I can't remember the fix as I normally work with character controllers. but I use this line of code for rotating
transform.Rotate (0,hmove * rotSpeed * Time.deltaTime,0);
hmove is a Var that holds Input.GetAxis("Horizontal")
When I changed the line to: if(Input.GetAxis("Vertical")) { yadda yadda yadda... }
It gives me the error of "cannot implicitly convert float to bool"
Okay, figured that part out; the line you gave SHOULD be something like this: if(Input.GetAxis("Vertical") > 0)...
Unfortunately, it didn't fix the issue so I'm still at square 1.
and you will everytime not thinking it all the way out when I typed that a IF ask if its true or not after cashing a couple Var to hold what you are getting from the inputs you can place a section of code like this
if(v$$anonymous$$ove > 0f || v$$anonymous$$ove < 0f){
curSpeed = maxSpeed * Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.forward *curSpeed* Time.deltaTime);
}
I used a raw axis as I wanted to know if they where pushing it or not didn't care abount numbers inbetween the final line reads transform.Translate(Vector3.forward * (either 1 or -1) time time.deltatime)
Thank you, bodec for your time and your suggestions. It sucks that they didn't fix the problem, but I really do appreciate your feedback all the same!
Answer by jpthek9 · Jan 07, 2015 at 03:27 AM
Instead of using Vector3.direction which returns the global right, you can use transform.direction.
I.e.
if(Input.GetKey(KeyCode.W))
{
rigidbody.AddForce(transform.forward * 1);
boosters.particleEmitter.emit = true;
}
if(Input.GetKey(KeyCode.D))
{
rigidbody.AddForce(transform.right * 1);
boosters.particleEmitter.emit = true;
}
This worked like a charm for what I was trying to do. I guess I was making it too complicated for my own good, but you were right. Changing it from Vector3 to transform.directional commands was the right move to make. Now, my little space station glides about in the open environment as if it were a hockey puck on ice lol. Thanks for the simplistic suggestion! Simple is always best anyways!
~$$anonymous$$organ
Answer by DwaynePritchett · Jan 07, 2015 at 05:29 AM
Vector3.Forward is in Global coordinates. Have you tried multiplying the rotation by the forward Vector? You can multiply Quaternions and Vectors, but on of them has to be on the left. I think it's Quaternion*Vector. But, try if(Pushed.W){ addForce(transform.rotation * forwardVector); }
Something like that should work. Sorry, I can't actually write it at the moment to double check.
Your answer
Follow this Question
Related Questions
Help with translation and rotation on a rigid body 2D 0 Answers
Rigidbody.AddForce and incorrect rotation 2 Answers
Add Force to the right of the rigidbody, not right of the screen 1 Answer
Stop A player Turning at Specific Point. 1 Answer
How to make a RigidBody not go into ground when tilted foward? 2 Answers