#Rotate the thingy

1 messages · Page 1 of 1 (latest)

edgy trench
#

@torn zinc So like this?

#

so what now?

torn zinc
#

What do you have?

edgy trench
#

a private float called targetAngle = 25;

torn zinc
#

Sorry I dont really have time or motivation to repeat everything

edgy trench
#

yess.

torn zinc
#

Show the code

edgy trench
#

1 sec hatebin buggy

#

I think pastebin is good for this

torn zinc
#

It doesnt have highlighting, pretty hard to read :/

edgy trench
#

yea, i know :/

#

but hatebin is so buggy now

#

it does not want to make the link

torn zinc
#

So where are you updating targetangle..?

#

Hint: nowhere

edgy trench
#

I want to update?

#

i think so

torn zinc
#

As I said

Set your targetAngle to -25, 0 or 25 according to what keys you are holding. A, nothing or D

edgy trench
#

ye

torn zinc
#

I didnt say "Use transform.Rotate"

edgy trench
#

But when its private float it cant be updated, no?

torn zinc
#

It can. Private just means that it cant be accessed from other classes

#

targetangle is supposed to change. Its the steering wheel's non-smoothed rotation

#

We then use it as the target angle for our smoothed rotation

edgy trench
#

aha.

#

So did I made this right? I dont think so

if (Input.GetKeyDown(KeyCode.A))
            {

                targetangle -= 25;
            }


torn zinc
#

That's not setting a value, that is subtracting from a value

edgy trench
#

oh

torn zinc
#

Tbh you should just use GetAxisRaw("Horizontal") to get your A-D input as a float afrom -1 to 1

#

Put that in a variable, lets say horizontalInput

#

Then you can just say targetAngle = horizontalInput * 25f

edgy trench
#

but I have an InputManager script

#

or something like that

#
private void FixedUpdate()
        {
            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
torn zinc
#

No idea what CrossPlatformInputManager is but use that instead of Input, if it works.

#

And if theres a reason to

edgy trench
#

crossplatformInputManager is like input but for mobile too.

#

i think

#

Uhm, so error code "cannot convert type float to bool"

torn zinc
#

It would be easier to help if i didnt have to keep asking for your current code

edgy trench
torn zinc
#

Just the lines you changed is enough

#

Especially the error line

#

Yeah dont put the GetAxisRaw in an if statement, it returns a float

torn zinc
#

Replace horizontalInput with the ..GetAxisRaw(...) or make it a separate variable

edgy trench
#

Okey. And how can I assign the gameobject to this?

torn zinc
#

I dont know what you mean by that

edgy trench
#

So When I press A or D the gameobject rotates

#

now just a codeline

torn zinc
#

How is your steering wheel rotated, can you show that

#

In the scene view with the object selected with local gizmos

edgy trench
#

But theres the problem, it is not rotating

#

because the steering wheel is not assigned to the codeline

#

i think

torn zinc
#

Yeah we arent there yet

#

What is the steering wheel's axis that it should rotate around?

edgy trench
#

It should rotate on the Y axis

torn zinc
#

Does your script have a reference to the steering wheel object?

edgy trench
#

yess.

torn zinc
#

Its not in english mostly so im not sure

edgy trench
#

yeah, its in Hungarian

torn zinc
#

Okay which object is that

edgy trench
#

sorry for that :/

#

Kkormany

torn zinc
#

Alright. Try setting its transform.localRotation to Quaternion.Euler(0, targetAngle, 0) for now

#

It wont be smoothed yet but you can see if the target rotation is correct

edgy trench
#

So put the transform.localRotation into an if thingy?

#

or how

torn zinc
#

Nah just add this linecs Kkormany.transform.localRotation = Quaternion.Euler(0, targetAngle, 0);

#

Quaternion.Euler creates a rotation with those angles. You only want Y so you leave the X and Z angles to zero.

edgy trench
#

Okkey, it rotates just fine!

#

But not smoothed yet

torn zinc
#

Nice. Okay now you can apply the smoothing to a second variable. Make a new private float, you can call it smoothedAngle or something. Now you can choose from a few ways of smoothing: MoveTowardsAngle, LerpAngle or SmoothDampAngle are functions in the Mathf class, you can choose one or try all and see whats best for you

#

-MoveTowards will change the at a constant speed
-Lerp will change the value quicker depending on how far it is from the target value.
-SmoothDamp has smoothing at the start and end, but its a bit more complicated to use (extra ref variable)

edgy trench
#

Okkey. So I think the MoveTowards will the best for this.

#

So I need to make a second variable.

#
targetangle = Input.GetAxisRaw("Horizontal") * 25f;

just like this?

#

but with Mathf?

#

noo. I just realised thats not how to do that

#

so a second variable

torn zinc
#

Use the targetAngle as the target value

#

And smoothedAngle as the current value

#

smoothedAngle = Mathf.MoveTowardsAngle(smoothedAngle, targetAngle...)

#

So you kind of update the smoothed angle from itself towards the target angle every frame

edgy trench
#

So is it correct?

smoothedAngle = Mathf.MoveTowardsAngle(smoothedAngle, targetangle, speed);


torn zinc
#

Yep, but you want to multiply speed with deltaTime if you didnt already. This is so that its the same speed no matter your FPS

edgy trench
#

so speed * Time.deltaTime?

#

So now it looks like

smoothedAngle = Mathf.MoveTowardsAngle(smoothedAngle, targetangle, speed * Time.deltaTime);
               targetangle = Input.GetAxisRaw("Horizontal") * 25f;

               Kkormany.transform.localRotation = Quaternion.Euler(0, smoothedAngle, 0);
torn zinc
#

Update the target angle before the smoothed angle. Other than that it looks ok

edgy trench
#

ok! Its just works so well!

#

Man, Thank you so much for your time and your help!

torn zinc
#

Nice 👍 No problem