Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

How can I make my custom humans tween toward the person who spawned them?

Asked by 5 years ago

Hey everyone! At the moment I am writing a class for everything that lives in a game I am making. This class is set up as follows currently:

  • Object is constructed with a SelfMesh (part in workspace)
  • Object is given properties (type, occupied, and age)

I have wrote a basic life function called Life:Animate() which at the moment allows the life form to perform five life choices and then they die.

While I am fairly confident the actual life cycle works - the issue seems to be in the possible life choice Life:GoToParent(), in which the life form is supposed to walk toward the person who spawned it. [tl;dr read this paragraph]

(currently the life forms seem to run toward the middle of the baseplate and start to pile up instead of running toward the person who spawned them)

This is the current setup, and my attempt [focus :Animate() and :GoToParent()]:

local Life = {}
Life.__index = Life

function Life.new(initType, trueObject, parent)
    local thisObject = {}
    setmetatable(thisObject, Life)

    thisObject.Type = initType
    thisObject.SelfMesh = trueObject
    thisObject.Guardian = parent
    thisObject.Occupied = false
    thisObject.Age = 0

    return thisObject
end

function Life:GoToParent()
    self.Occupied = true

    local Parent = game:GetService("Players"):FindFirstChild(self.Guardian.Name).Character.HumanoidRootPart
    local SolvedCFrame = CFrame.new(self.SelfMesh.CFrame.p, Parent.Position)
    local Direction = (self.SelfMesh.Position).Unit

    game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = Vector3.new(self.SelfMesh.Position + Direction)}):Play()

    wait(5)

    self.Occupied = false
end

function Life:Animate(p)
    self.SelfMesh.CFrame = CFrame.new(p) * CFrame.new(0,1,0)

    repeat
        ----------- Life Cycle
        local LifeChoice = Random.new():NextInteger(1,1)

        if LifeChoice == 1 then
            self:GoToParent()

            repeat wait() until self.Occupied == false
        end

        self.Age = self.Age + 1
        -----------
    until self.Age == 5

    self:Death()
end

function Life:Death()
    self.SelfMesh:Destroy()
    self = nil
end

return Life

All help is appreciated, thank you!

1 answer

Log in to vote
6
Answered by
nilVector 812 Moderation Voter
5 years ago
Edited 5 years ago

On this line:

game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = Vector3.new(self.SelfMesh.Position + Direction)}):Play()

You are constructing a new Vector3 with a Vector3 inside of it rather than trying to give it an actual position with numbers. Hence, it is interpreting it as position 0, 0, 0, which is the center of the baseplate. Getting rid of the Vector3.new() should fix this:

game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = self.SelfMesh.Position + Direction}):Play()

This solves your problem, but I see that Direction is a unit vector, which means its size is only 1 stud, so it won't travel too far from self.SelfMesh.Position. Just a heads up.

Additionally, I would like to suggest making use of the .Completed event of Tween rather than explicitly waiting out the length of the tween. Something like this should suffice:

local tween = game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = self.SelfMesh.Position + Direction})
tween:Play()
tween.Completed:Wait()
0
Ah yes -- I forgot to multiply the unit vector, I had meant to. Anyway; let me give this a try and I'll tell you how it goes. SummerEquinox 643 — 5y
0
Alright so now they seem to go the opposite direction - which leads me to believe I have made an error in determining the directional vector or something. I'm not the best with this stuff. Outcome: https://imgur.com/a/FRFB9Y4 ;; New function: https://hastebin.com/ademumuveb.rb SummerEquinox 643 — 5y
0
You want the SelfMesh to move towards the Guardian, right? Then in the MoveTween definition, you can simply set the Position equal to the position of the Guardian, and that's where it will tween towards. nilVector 812 — 5y
0
Oh that's true. Man I really suck with this lol. Thank you. SummerEquinox 643 — 5y
Ad

Answer this question