#whats the problem here
1 messages · Page 1 of 1 (latest)
so, you have a Tracking component
"script" is kind of nebulous
oh
i don't like the term
Tracking is a component 'cause you can attach it to a game object
fair enough
but yeah, it has a launcher field
i was following this tutorial btw:
You could say monobehavior ?
that works
that would also be valid
basically to give you an idea of what im trying to make
i want the NPC turret to turn at the player
does the launcher object exist in the scene before the game starts?
and then spawn a projectile and fire it
yeah
i placed it there in the editor (or whatever it's called)
and does the object that Tracking is on also exist in the scene?
yes, just hidden
hidden?
these two unchecked
ah
i dont think that matters though?
it does not, no
it's attached to the launchOrigin which is under the launcher object in hierarchy
start the game, then look at the object that Tracking is on
alr
the "Launcher" field should have a reference in it
double click on it to be taken to that object
it does
okay, what now
good, that object has a rigidbody on it
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
yeah, null reference
the error is on that line but i dont think that line is the issue
maybe it's not recognizing i've attached the Launcher object to the SerializeField thing
lemme restart unity
no, that is indeed the line that's causing the error if that's the case
it's trying to access a null object
the field is clearly assigned or you'd get an error in Start()
which means a prior line is failing to correctly .GetComponent<RigidBody>()
yes, because _launchRb is null
because launcher.GetComponent<Rigidbody>() would throw an NRE if launcher was null
ah, i see
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
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
it is possible that you hadn't reloaded scripts (if you turned off automatic reloading)
each time i alt-tab back to unity (from jetbrains rider) i see a progress bar that says something about rebuilding/reloading or something
so i think it does auto reload
yeah, that's the script reloading
ah
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)
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));
yes, LookRotation
or should i be doing {GameObject}.LookRot and not rb.LookRot
ah ok
MoveRotation is a non-static method onTransform
which changes the transform's rotation
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);
you can use Quaterion.RotationTowards to move from one rotation to another
ah ok
with a specified max angle change
so Quaternion.RotateTowards(a, b, 45 * Time.deltaTime) would move by 45 degrees per second
euler angles can cause weird surprises
ah
you might have noticed how rotations can suddenly set several axes to -90, and stuff like that
is that why im unable to rotate past 90 degrees up (vertical) and flip over without rotating horizontally?
because eulerangles doesnt like that?
i dunno about that
okay
ill try rotationtowards
okay so _launchRb.MoveRotation(Quaternion.LookRotation(diff)); works, for the launcher
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
it's a child
oh
if it's fixed to the gun barrel, then it will move and rotate with the barrel
so you're saying children shouldnt be sent to fight in wars
do you copy its rotation to the bullet when instantiating it?
oh ok
oh yeah i do
GameObject proj = Instantiate(projectile, raw.position, raw.rotation);
if so, then use, AddForceRelative would behave reliably
note that AddRelativeForce(400, 0, 0) applies a force to the right
it might be better to use ForceMode.Impulse, like:
rb.AddRelativeForce(0, 0, 10, ForceMode.Impulse);
the bullet is aligned with the barrel on the x-axis
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
ah ok
i was doing ForceMode.Acceleration initially
since i didnt wanna deal with mass and stuff
Acceleration is like the default mode, but it ignores mass
i just want it to go at a specific velocity lol
oh ok
so rb.AddRelativeRorce(10, 0, 0, ForceMode.VelocityChange) would increase the velocity by 10 meters to the right
acceleration is meant to be applied for many ticks
ah.
it changes velocity by that much per second that you're applying it
cool, ty :)
ah
anyway, gotta scram now! you can leave other questions here if you need to
but here it doesnt really matter, does it
because each Update() loop spawns a new one