#Spawn primitive and parenting it to the player

1 messages · Page 1 of 1 (latest)

deft current
#

I don't understand why my code doesn't work. It sometimes gives a null error. What am I doing wrong?

public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            var player = Player.Get(sender);
         
   var cube = PrimitiveObjectToy.Create();
            cube.Type = PrimitiveType.Cube;
            cube.Scale = new Vector3(0.2F, 0.2F, 0.2F);
            cube.Parent.SetParent(player.GameObject.transform);
            cube.Flags = AdminToys.PrimitiveFlags.Collidable;

            response = cube.ToString();
            return true;
            



        }
quiet cedar
#

cube.Parent = player.GameObject;

honest fossil
#

ideally you'd use PrimitveObjectToy.Create(Vector3.zero, Quaternion.identity, Vector3.one * 0.2f, player.GameObject.transform)

deft current
honest fossil
quiet cedar
#

You trying to change the cube parent parent which doesn't exist

deft current
honest fossil
#

np

deft current
honest fossil
#

set the position

#

it's local, so Vector3.up will be up by 1 unit relative to the parent

deft current
honest fossil
#

pass it to create

#

you don't need to access the transform for this anyway

#

-# the naming is kinda dumb but primitive.Position gets/sets the local position

honest fossil
#

np

deft current
# honest fossil np

Sorry to bother you, I want to make sure I got it right

Did you mean to do it this way?

var player = Player.Get(sender);
var cube = PrimitiveObjectToy.Create(player.GameObject.transform);
cube.Type = PrimitiveType.Cube;

cube.Position.Set(0F, 1.5F, 0);
cube.Scale.Set(0.2F, 0.2F, 0.2F);
cube.Flags = AdminToys.PrimitiveFlags.Collidable;

response = cube.ToString();
return true;

deft current
jovial lagoon
#

Use Position =

#

But also

#

.Create(pos, transform)

#

or smth

stiff glen
#

You appear to have an over-reliance on using functions to set variables

#

Like you did cube.Parent.SetParent, cube.Position.Set, and cube.Scale.Set

#

Stop doing that

#

Don't think default for these things that you need to use a .Set or anything like that

#

Just do things like
cube.Parent = parent
cube.Position = position
cube.Scale = scale

#

I mean

#

It's possible using .Set here works

#

But I wouldn't rely on it

#

cube.Position returns a UnityEngine.Vector3 object
Calling .Set(x, y, z) on that could modify the original instance, but it could be modifying a cloned instance

#

Second thing

#

You never spawned the cube on the network

#

Do cube.Spawn() after you finish your initialization

#

Third thing

#

You're also still using cube.Position and not local position

stiff glen
#

But you also shouldn't be spawning it before you finish making it

stiff glen
#

If you want the cube to be purely visual and not able to be collided with, you want to set the flags to AdminToys.PrimitiveFlags.Visible
I'm going to make the assumption that you wanted both

#
var player = Player.Get(sender);
var cube = PrimitiveObjectToy.Create(player.GameObject.transform, false);
cube.Type = PrimitiveType.Cube;
cube.Transform.localPosition = new Vector3(0, 1.5f, 0);
cube.Scale = new Vector3(0.2f, 0.2f, 0.2f);
cube.Spawn();
response = cube.ToString();
return true;
#

Now keep in mind, the player is 1.8u tall and the player position is 0.96u tall
This means you're placing your cube 0.66u above the player's head
Accounting for the scale you gave, the gap between the top of the player's head and the cube should be 0.56u

honest fossil
#

Stupid naming but what can we do
Break every plugin?

stiff glen
#

Fucking serious

#

That's the dumbest shit I've heard

honest fossil