generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Getting Player to face direction of mouse

Previous Thread :: Next Thread 
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
14 Aug 2014 12:45 PM
I want the effect of the player facing the mouse like in Dungeon Delver, while walking on the ground. I tried using BodyGyro, but the closest I got was getting the part to face the same direction as the mouse.

What specifically should I do to accomplish this?
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
14 Aug 2014 06:15 PM
Bump
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
14 Aug 2014 07:50 PM
Bump 2
Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
14 Aug 2014 08:39 PM
Here I made this
http://www.roblox.com/Point-towards-mouse-item?id=172134435
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
14 Aug 2014 09:12 PM
You could like, use MoveTo() with the model O:


sel = nil

tool.Equipped:connect(function(mouse)
sel = true
while sel == true do
playermodel:MoveTo(playermodel.Position,mouse.Hit.Position)
wait()
end
tool.Unequipped:connect(function()
sel = false
end) end)
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
15 Aug 2014 12:34 PM
Thanks for the model! Can you explain how it works?
Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
15 Aug 2014 02:25 PM
You mean how to use it? Just put it in workspace.

How it works? Well, first I get the vector 'dir' from the torso to the mouse and normalize it (magnitude=1). Then, I find the angle 'dir' makes with the positive ( I think) Z-Axis by doing math.atan2(dir.X, dir.Z)

note: math.atan2(opposite, adjacent) is just atan(opposite/adjacent) but in the right quadrant and handles adjacent being 0 correctly.

Then I just add 180 degrees (or math.pi) to the angle to line it up correctly:

CFrame.new(torsoPos)*
CFrame.Angles(0, math.atan2(dir.X, dir.Z) + math.pi, 0)
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
15 Aug 2014 03:11 PM
Oh ok cool! I don't quite get it, but thanks! Is there an easier method to do this that I can understand better? Why doesn't BodyGyro work for this?
Report Abuse
travddm is not online. travddm
Joined: 29 Mar 2012
Total Posts: 926
15 Aug 2014 03:14 PM
BodyGyro *does* work for this.. CFrame.new(torsopos,mouse.Hit) as the CFrame for a BodyGyro with a maxTorque of Vector3.new(0,whateveryouwant,0)
Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
15 Aug 2014 03:41 PM
I had some issues with BodyGyro when you try to jump, and thought CFrame worked better.
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
16 Aug 2014 09:40 AM
Oh ok thanks a ton! Is there a way to make the player able to jump with BodyGyro?
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
16 Aug 2014 10:17 AM
@nicemike
That sounds overcomplicated. I think this would work just as well:

_, y = CFrame.new(Character.Torso, Mouse.Hit.p):toEulerAnglesXYZ()
Character.Torso.CFrame = Character.Torso.CFrame * CFrame.Angles(0, y, 0)
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
17 Aug 2014 11:21 AM
Oh ok lol, I get that fully besides the toEulerAnglesXYZ() part, what does that do?
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
17 Aug 2014 11:27 AM
It's much simpler than any of this. Did you know that if you use CFrame to move or rotate a part, anything welded or joined to it will follow?

Your Torso is joined to all of your other body parts. All you have to do is this:

Torso.CFrame = CFrame.new(Torso.Position, Vector3.new(Mouse.Hit.p.x, Torso.Position.Y, Mouse.Hit.p.z))

When you do CFrame.new(Position1, Position2), it will make a new CFrame where the part is put at Position1 and LOOKS AT Position2.

I did Vector3.new(Mouse.Hit.p.x, Torso.Position.Y, Mouse.Hit.p.z)) where Position2 is so it would only make your torso turn, and not look up or down. You don't want it to look down or up, but only need it to turn left or right.
Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
17 Aug 2014 12:51 PM
That's probably easier to understand, I just prefer my way as a stylistic choice.

BTW, if you do yours correctly (localize all variables), it's just over 1% faster than mine because it's C-side.
Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
17 Aug 2014 12:54 PM
Scratch that, which one is faster changes erratically.
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
17 Aug 2014 01:18 PM
OMG than you so much it worked AND I understand it fully lol. Thanks!
Report Abuse
travddm is not online. travddm
Joined: 29 Mar 2012
Total Posts: 926
28 Aug 2014 11:51 AM
BodyGyro would and does work fine, you simply have to set the P(Power) and D(Dampening) to integers that work nicely with each other.
Report Abuse
IthacaMedia is not online. IthacaMedia
Joined: 25 Jan 2014
Total Posts: 1337
29 Aug 2014 09:10 AM
Oh ok.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image