#whats the problem here

1 messages · Page 1 of 1 (latest)

opal crater
#

.

limber drum
#

one sec ill copy paste my original question

#

if i can find it

opal crater
#

so, you have a Tracking component

limber drum
#

component as in script?

#

or are you referring to something else

opal crater
#

"script" is kind of nebulous

limber drum
#

oh

opal crater
#

i don't like the term

#

Tracking is a component 'cause you can attach it to a game object

limber drum
#

fair enough

opal crater
#

but yeah, it has a launcher field

limber drum
#

i was following this tutorial btw:

opal crater
#

which references a game object

#

that game object has a Rigidbody on it.

limber drum
#

up to step 3, at which i started coding my own thing

hasty linden
limber drum
#

that works

opal crater
limber drum
#

basically to give you an idea of what im trying to make

#

i want the NPC turret to turn at the player

opal crater
#

does the launcher object exist in the scene before the game starts?

limber drum
#

and then spawn a projectile and fire it

limber drum
#

i placed it there in the editor (or whatever it's called)

opal crater
#

and does the object that Tracking is on also exist in the scene?

limber drum
#

yes, just hidden

opal crater
#

hidden?

limber drum
#

these two unchecked

opal crater
#

ah

limber drum
#

i dont think that matters though?

opal crater
#

it does not, no

limber drum
#

it's attached to the launchOrigin which is under the launcher object in hierarchy

opal crater
#

start the game, then look at the object that Tracking is on

limber drum
#

alr

opal crater
#

the "Launcher" field should have a reference in it

#

double click on it to be taken to that object

limber drum
opal crater
#

good, that object has a rigidbody on it

limber drum
opal crater
#

and the error is, indeed, on this line?

Vector3 orig = _launchRb.position;
#

if you double click on the exception in the log, your code editor should open the file and put the cursor on that line

limber drum
#

yeah, null reference

limber drum
#

maybe it's not recognizing i've attached the Launcher object to the SerializeField thing

#

lemme restart unity

opal crater
#

no, that is indeed the line that's causing the error if that's the case

limber drum
opal crater
#

the field is clearly assigned or you'd get an error in Start()

limber drum
#

which means a prior line is failing to correctly .GetComponent<RigidBody>()

opal crater
opal crater
opal crater
#

you should make sure you don't have an error like that, though

#

you might have 1 error from Start and then a huge pile from Update

#

turning on Collapse in the console can help with that

limber drum
#

one sec unity is still starting

#

great, now it works... ugh

#

hate when i dont know what fixed it

#

i think ill just restart unity more often

opal crater
#

it is possible that you hadn't reloaded scripts (if you turned off automatic reloading)

limber drum
#

so i think it does auto reload

opal crater
#

yeah, that's the script reloading

limber drum
#

ah

opal crater
#

i would not expect this to happen randomly for no reason; i certainly haven't run into that before (just things that seem like it)

limber drum
#

hmm ok then

#

anyways, is there a way to convert a Vector3 to a look direction?

#

currently i'm doing this```cs
Vector3 dest = _playerRb.position;
Vector3 orig = _launchRb.position;
Vector3 diff = dest - orig;
_launchRb.MoveRotation(Quaternion.Euler(diff));

opal crater
#

yes, LookRotation

limber drum
#

ah.

#

wait what?

#

the method doesnt exist

opal crater
#

Quaternion.LookRotation

#

it's a static method on Quaternion

limber drum
#

or should i be doing {GameObject}.LookRot and not rb.LookRot

limber drum
opal crater
#

MoveRotation is a non-static method onTransform

#

which changes the transform's rotation

limber drum
#

oh ok

#

speaking of LookRotation

#

if im trying to rotate my ship at a fixed rate depending on mouse position on-screen

#

should i use LookRotation for that or am i better off manually doing += to the angle like this and converting quaternion <-> Vector3:```cs
Vector3 mousePos = Input.mousePosition;
float lateralRot = mousePos.x / Screen.width * 2 - 1;
float verticalRot = mousePos.y / Screen.height * 2 - 1;

        Vector3 curLookDir = ship.rotation.eulerAngles;
        curLookDir.y += lateralRot * Sens * Time.deltaTime;
        curLookDir.x += verticalRot * Sens * Time.deltaTime;
        ship.rotation = Quaternion.Euler(curLookDir);
opal crater
#

you can use Quaterion.RotationTowards to move from one rotation to another

limber drum
#

ah ok

opal crater
#

with a specified max angle change

#

so Quaternion.RotateTowards(a, b, 45 * Time.deltaTime) would move by 45 degrees per second

limber drum
#

would it be any improvement over doing my current code?

#

(which works just fine btw)

opal crater
#

euler angles can cause weird surprises

limber drum
#

ah

opal crater
#

you might have noticed how rotations can suddenly set several axes to -90, and stuff like that

limber drum
#

is that why im unable to rotate past 90 degrees up (vertical) and flip over without rotating horizontally?

#

because eulerangles doesnt like that?

opal crater
#

i dunno about that

limber drum
#

okay

#

ill try rotationtowards

#

okay so _launchRb.MoveRotation(Quaternion.LookRotation(diff)); works, for the launcher

opal crater
#

oh wait, _launchRb is a rigidbody, not a transform, whoops

#

but yeah that's right

limber drum
#

since the (invisible) projectile origin is a sub-object (if thats the right term) i can just always launch a projectile forwards by doing bullet.AddRelativeForce(400, 0, 0); and not worry about which way the gun is facing right

opal crater
#

it's a child

limber drum
#

oh

opal crater
#

if it's fixed to the gun barrel, then it will move and rotate with the barrel

limber drum
#

so you're saying children shouldnt be sent to fight in wars

opal crater
#

do you copy its rotation to the bullet when instantiating it?

limber drum
#

GameObject proj = Instantiate(projectile, raw.position, raw.rotation);

opal crater
#

if so, then use, AddForceRelative would behave reliably

limber drum
#

okay cool

#

ill just make the turret turn smoothly now :)

opal crater
#

note that AddRelativeForce(400, 0, 0) applies a force to the right

opal crater
#

it might be better to use ForceMode.Impulse, like:

rb.AddRelativeForce(0, 0, 10, ForceMode.Impulse);
limber drum
#

the bullet is aligned with the barrel on the x-axis

opal crater
#

the default mode is a force

#

so it says "push on this with 400 units of force for this physics step"

#

the amount of velocity you get out of that would depend on how long each physics step is

#

not a huge deal, since you generally don't change that

limber drum
#

ah ok

opal crater
#

but impulse makes more sense for shooting something

#

it's an instant impulse

limber drum
#

i was doing ForceMode.Acceleration initially

#

since i didnt wanna deal with mass and stuff

opal crater
#

Acceleration is like the default mode, but it ignores mass

limber drum
#

i just want it to go at a specific velocity lol

opal crater
#

use VelocityChange, then

#

VelocityChange ignores mass and is instant

limber drum
#

oh ok

opal crater
#

so rb.AddRelativeRorce(10, 0, 0, ForceMode.VelocityChange) would increase the velocity by 10 meters to the right

limber drum
#

is ForceMode.Acceleration not instant?

#

does it not happen within a single tick

opal crater
#

acceleration is meant to be applied for many ticks

limber drum
#

ah.

opal crater
#

it changes velocity by that much per second that you're applying it

limber drum
#

cool, ty :)

opal crater
#

you just want to change the velocity, period

#

thus, VelocityChange

limber drum
#

ah

opal crater
#

anyway, gotta scram now! you can leave other questions here if you need to

limber drum
#

because each Update() loop spawns a new one