#Ship completely broken

1 messages · Page 1 of 1 (latest)

soft hearth
#

I am completely beside myself about what's happening here. The IsGrounded is not working as I want it to and the velocity goes berserk when I try moving. Why is this happening and how do I fix this?

Code:


public class ShipPhysics : MonoBehaviour
{
    public float MaxSpeed;
    public float Speed;
    public float MinSpeed;
    public float Acceleration;
    public float Deceleration;
    public float Handling;
    public Rigidbody rb;
    public float GravitySpeed;
    public bool IsGrounded;
    public bool IsGrounded2;
    public float Floatheight;
    public float Drag;
    int LayerCoolmask;
    int LayerCoolmask2;
    private void Awake()
    {
        LayerCoolmask = LayerMask.GetMask("Ground");
        LayerCoolmask = LayerMask.GetMask("Maglock");
    }
    void FixedUpdate()
    {
        IsGrounded = Physics.Raycast(transform.position, -transform.up, Floatheight, LayerCoolmask);
        IsGrounded2 = Physics.Raycast(transform.position, -transform.up, Floatheight, LayerCoolmask2);
        if (!IsGrounded2 && !IsGrounded)
        {
            GravitySpeed += Physics.gravity.y * Time.deltaTime;
        }
        else
        {
            GravitySpeed = 0;
        }
        if (Input.GetAxis("Vertical") > 0)
        {
            Speed += Acceleration * Input.GetAxis("Vertical") * Time.deltaTime;
        }
        if (Input.GetAxis("Vertical") < 0)
        {
            Speed += Deceleration * Input.GetAxis("Vertical") * Time.deltaTime;
        }
        else
        {
            Speed -= Drag * Time.deltaTime;
        }
        if (Speed > MaxSpeed)
            Speed = MaxSpeed;
        if (Speed < MinSpeed)
            Speed = MinSpeed;
        rb.angularVelocity = transform.right * Input.GetAxis("Horizontal") * Handling * Mathf.Deg2Rad;
        rb.velocity = (transform.forward * Speed) - (Vector3.down * GravitySpeed);
    }
}```
#

<@&823670198958948373>

sick trench
#

Can you describe your problem more precisely than berserk

soft hearth
#

Also the origin is way off where it's position assumes

#

I think the origin is the problem, but I don't know how to fix it

#

The origin is not the problem, the origin is simply an error in how the origin is calculated (it's because of the camera)

flat stag
#

why do you have gravityVelocity if the rigid body already has gravity

soft hearth
#

Because using rb.velocity.y makes the ship fly higher if you accelerate up a slope

#

The problem (I think) is the IsGrounded.

#

Same with GravitySpeed, actually, after a test with a GetMask.

#

Nevermind

sand glen
#

😨

soft hearth
#

So, how do I get the Layermask to not have to use GetMask? I've tried the bit shift method and the single GetMask already, but neither of them worked.

#

*every single frame

hushed talon
#

in the Awake function you set the LayerCoolmask variable to two different values

soft hearth
#

Oh

hushed talon
#

i believe you're trying to add the two layer masks to the same variable

#

but I don't think this is how it works

soft hearth
#

Thank you

hushed talon
#

i believe it'd be easier if you made the variable public and set its value from the inspector

robust relic
#

Yep, I think it would be easier :

[SerializeField] private LayerMask LayerCoolmask;
[SerializeField] private LayerMask LayerCoolmask2;
...

And you could assign them very easy in the inspector

empty marsh
#

Rip ship

#

It sank

soft hearth
azure kindle
#

I mean using the inspector is fine but I'll send you something later that should help

azure kindle
# soft hearth <:blobfacepalm:815271316289880105>

LayerMasks are just integers, you can make your own doing stuff like this

public class GroundComponent
{
   private GroundSurface groundType;
   public GroundSurface GroundType => groundType;
   
  public static GroundSurface SetFlag(GroundSurface a, GroundSurface b)
  {
    return a | b;
  }
  public static GroundSurface UnsetFlag(GroundSurface a, GroundSurface b)
  {
    return a & (~b);
  }
  public static bool HasFlag(GroundSurface a, GroundSurface b)
  {
    return (a & b) == b;
  }
  public static GroundSurface ToggleFlag(GroundSurface a, GroundSurface b)
  {
    return a ^ b;
  }
}
[System.Flags]
public enum GroundSurface
{
    None = 0,
    Default = 1,
    Rough = 2,
    Slippery = 4,
    Dangerous = 8

    Spiked = Rough | Dangerous;
}
#

If you need an explanation on what this is doing just ping me

empty marsh
azure kindle
empty marsh
#

how do u make a thread in a forum post