last day (17 days later) » 

4:52 PM
1
A: Realistic BOAT/SHIP movement and rotation (2d)

maksymiukThere's two parts to this problem, which should be kept completely separate from each other and at no time does the equation of one interfere with the equation of the other. The first is the forward movement of the ship, which can be achieved in two ways: If you are using a rigidbody void Prop...

 
Thanks a lot for your detailled answer, i'm gonna try to make it work and get back to you asap
Whatever i try (with or without Vector3), it keep telling me : "Static member `UnityEngine.Vector3.forward' cannot be accessed with an instance reference, qualify it with a type name instead", my code : RigidBody.AddForce(new Vector3(targetPosition.x, targetPosition.y, targetPosition.z).forward * speed * Time.deltaTime);
 
forward is a static field. To call forward you can only do it like this: Vector3.forward.
 
I just tried again without replacing "transform.forward" by my target Vector3, and i don't have any error, but the boat doesn't move and no axis is being changed, here is the code : float speed = 150f; Rigidbody2D rb = GetComponent<Rigidbody2D>(); Transform transform = GetComponent<Transform>(); rb.AddForce(transform.forward * speed * Time.deltaTime); i of course disabled "is kinematic" and have a mass of 1
 
Make the speed 150000000 and see if there is a difference. It is probably just moving extremely slowly
 
I tried with 150000000 and even 1500000000000, but the coordinates in the Transform component do not change :s
 
4:52 PM
And you are running this code every frame? Did you set the value through inspector, if it is global? (Inspector values don't update through code).
 
Yep, in the Update() function : imgur.com/a/AHjxW
 
if you do a Debug.Log(transform.forward) what does it say?
also if you try to just do rb.AddForce(new Vector3(1500000000000f, 0f, 0f)); and that doesnt work, then something is wrong with your rigidbody or how it is set up
or you have other movement that is overriding it
 
 
2 hours later…
6:34 PM
Sorry i was afk, i'm gonna try that right now
1) When i do a Debug.Log(transform.forward), i get (0.0, 0.0, 1.0)
2) When i try rb.AddForce(new Vector3(1500000000000f, 0f, 0f)); it move (fast of course but it does)
My initial code was :

`transform.position = Vector3.MoveTowards (transform.position, new Vector3(targetPosition.x, targetPosition.y, 0f), _movingSpeed * Time.deltaTime);`

`targetPosition` is the transform.position of where i click
i can't figure out how to implement rb.AddForce(new Vector3(1500000000000f, 0f, 0f)); with my initial code and target position
 
 
1 hour later…
8:07 PM
Dont use AddTorque and Vector3.MoveTowards together
one is a forced movement one is just a force on the rigidbody, they dont mix well
either use AddForce and Addtorque, or Vector3.MoveTowards and Transform.Rotate
 
I use your first example actually
float speed = 150f;
Rigidbody2D rb = GetComponent<Rigidbody2D>();
Transform transform = GetComponent<Transform>();
rb.AddForce(new Vector3(transform.forward) * speed * Time.deltaTime);
 
that worked right?
 
nope i have a compile error
Assets/Scripts/BoatMovement.cs(96,60): error CS1729: The type UnityEngine.Vector3' does not contain a constructor that takes 1' arguments
this is about this line :
rb.AddForce(new Vector3(transform.forward) * speed * Time.deltaTime);
 
yea because transform.forward is the Vector3
no need to make a Vector3(vector3)
 
indeed
 
8:13 PM
the correct way would be rb.AddForce(transform.forward * speed * Time.deltaTime);
 
but then i come back to the first situation
the boat isn't moving
 
does neither one move
so it wont move OR rotate?
 
it wont move actually (i didn't try the rotate part for now)
But if i do
rb.AddForce(new Vector3(1500000000000f, 0f, 0f));
it move
 
Try rb.AddForce(transform.left * speed * Time.deltaTime);
or Try rb.AddForce(transform.up * speed * Time.deltaTime);
with a big speed
then try these 3
 
it move!
 
8:16 PM
rb.AddForce(Vector3.left * speed * Time.deltaTime);
rb.AddForce(Vector3.up * speed * Time.deltaTime);
ohh okay
so it just didnt want to move forward for some reason
it was on the wrong axis
 
ha ?
did you notice that i change Rigidbody to Rigidbody2D ?
 
maybe because forward on a 2d plane is literally forward away from you
 
i don't know if it does matter
oh
 
well yea it will restirct it to 2 dimensions
thats about it
 
oh okey
so we can't use it in 2D ?
 
8:18 PM
well you want 2d yes
if your game is 2d
 
yes, as like the GIF in my post :)
 
then use rigidbody2d
 
so basically
you dont want your movement script to interact with the turning script
you always want it moving forward no matter whaat
 
and then the roation script will handle where it goes right ?
progressivly
 
8:19 PM
since boats cannot move diagonally or sideways, how it is turning has nothing to do with its ability to move forward
 
yes i agree
 
so just make it move forward, then have another method to adjust which way that forward points
like in my original post
 
yep i got that but how ? since .forward is not working (i use your example in the post)
 
well your forward is just different than unity's forward
the forward of the ship is like transform.left or something
its either one of these: transform.left, -transform.left, transform.up, -transform.up
one of those four will be your ship's forward
 
but it will go straigth to TOP LEFT RIGHT OR BOTTOM, not straigth in the current direction (whatever roration it have)
 
8:23 PM
then use transform.forward for the AddTorque
it will
when you turn it, transform.up or transform.right will adjust accordingly
thats why it works this way
just turn it like 45 degrees, and tell me what Debug.Log(transform.up) will say
also same with transform.right
i mean left
 
sorry i'm trying to understand everything, give me 2 min
for just moving forward i should do :

float speed = 150f;
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.AddForce(transform.up * speed * Time.deltaTime);

And then add a rotation based on my targetPosition ?
I tried like that :

float speed = 150f;
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.AddForce(transform.up * speed * Time.deltaTime);

rb.AddTorque(targetPosition.z * speed * Time.deltaTime);
But ship is rotating like crazy on itself while going to the destination :D
oh i got something, while reducing the rotation speed
(btw ship continue moving/rotating, even after stop calling the function)
 
8:42 PM
Addtorque will be a bit more complicated than just a target position thing
but first of all your passing a float into AddTorque you need to pass a Vector3
 
oh ? that's weird because when i pass a Vector3 it says i should pass a float
Argument #1' cannot convert UnityEngine.Vector3' expression to type `float'
 
ohh because its 2d
okay so in that case...
its still a bit more complex
sec
 
ok thanks for taking the time to help me btw :)
 
not a prob
first of all you need something to determine when your boat is finished moving
 
i have a GameObject with a collider, and when the boat get out of the collider i stop calling the function in my Update(). But i just noticed that the boat stiull move and rotate even after stop calling the function oO
 
8:48 PM
id have something like if (Vector2.Distance(target, transform.position) <= threshold) Move();
yea or collider
put all your movement logic into a Move() function, which you only call if your not in the collider
 
ok
 
so it would look like this: void Update() { if (moving) Move(); }
void Move() { Propel(); Rotate(); }
 
ok i follow for now
 
and in your OnTriggerEnter you can do moving = false;
then OnTriggerExit you can put moving = true;
so if it floats away from the trigger it will come back
 
ok that's what i already did (it was working without the addforce/addtorque), but since i added theses two, it ignores the fact that i don't call Move(), and keep turning/moving
imgur.com/a/Td20t see the green circle ? this is the collider, as soon as the boat exit it, it will stop call the Move(); function, but as you can see it keep moving/turning
but i think it's just because addforce and addtorque keep stacking too much before we stop it
 
8:57 PM
turnSpeed is too high
 
even when reducing it to one, the boat still going on even after not calling MOVE();
 
your addtorque should look like this rb.AddTorque(direction * speed * Time.deltaTime);
where direction is only either 1 or -1
 
i have
rb.AddTorque(targetPosition.z * turnSpeed * Time.deltaTime);
when i debug log
targetPosition.z
 
nono not targetposition.z
 
i get
-10
oh
 
9:02 PM
direction should be only either 1 or -1
1 would be rotating to the right, -1 would be rotating to the left
or vice versa
 
indeed
so i just put
rb.AddTorque(-1f * turnSpeed * Time.deltaTime);
for the test
 
sure
but also 1 more thing
sec
 
ok
 
Vector2 angle = Vector2.Angle(targetPosition - transform.position).normalized, transform.up);
if (angle > threshold)
rb.AddTorque(-1f * turnSpeed * Time.deltaTime);
sorry angle is a float here
float angle = Vector2.Angle(targetPosition - transform.position).normalized, transform.up);
if (angle > threshold)
rb.AddTorque(-1f * turnSpeed * Time.deltaTime);
Thats how it should work
so what that is doing is it will only turn if its not facing towards your point
 
ok i'm trying
 
9:07 PM
if it is facing theres no reason to turn anymore
 
oh i understand, makes sense
 
you need to declare a float threshold
some small number
 
ok i try 1f
So this is what i get : imgur.com/a/jMGca
with :
void Move() {

float speed = 50f;
float turnSpeed = 5f;
Rigidbody2D rb = GetComponent<Rigidbody2D>();

rb.AddForce(transform.up * speed * Time.deltaTime);

float threshold = 1f;
float angle = Vector2.Angle((targetPosition - transform.position).normalized, transform.up);
if (angle > threshold)
rb.AddTorque(-1f * turnSpeed * Time.deltaTime);
}
 
increase turn speed
a bit
 
keeps turning
 
9:14 PM
okay
can you add Debug.Log(angle) right under it and then do a dump of the log here as its turning
give me like 20 lines or so
 
169.1028
172.1844
175.281
178.3926
178.3926
178.4625
175.3028
172.1297
168.9431
168.9431
165.7258
 
right under that angle declaration
try this
float direction 1f;
if (angle > 0 && angle < 180) direction = -1f;
then use rb.AddTorque(direction * turnSpeed * Time.deltaTime);
 
so like this right :
float angle = Vector2.Angle((targetPosition - transform.position).normalized, transform.up);

float direction = 1f;
if (angle > 0 && angle < 180) direction = -1f;

if (angle > threshold)
rb.AddTorque(direction * turnSpeed * Time.deltaTime);
 
yes
 
it give me this
 
9:25 PM
hmm okay i think Vector2.Angle doesnt work how im expecting it to
sec
 
would like me to create a new project with only this part, so you can try more easly what you have in mind ?
 
nah its cool
give me a little while ill figure this out
i know what the problem is
 
ok thanks !
 
no prob
 
well if you change your mind, i made a simple example for you s000.tinyupload.com/…
 
9:52 PM
i'm sorry but i have to go to sleep, i'm working in a few hours, i come back asap
thank you very much again for your help
 
no prob
 

  last day (17 days later) »