- Home /
Rotating GameObject Around Player With "Mouse Y" Position
Good Morning/Afternoon/Evening ... Night :)
So I've currently in the process of scripting out a simplistic game mechanic, whereupon the main player can pick up/throw and rotate the desired gameobject at will. And that's all going relatively well.
Only I also want the desired Gameobject to be able to rotate around the character depending on where the Mouse cursor is currently centered. So essentially, the Gameobject should seem as if it's following the Y-Axis (Or camera angle) of the Mouse Cursor, by rotating in an circular arc around the main player.
Many Thanks in advance, And I do merely apologise if this is a relatively naive question to ask, I'm still in initial learning stages unfortunately. So do know any feedback/criticism or casual insults would be much appreciated :)
(Also, If you're wanting the pickup/throw/rotate code for your own scripts, do merely ask, and I'll be sure to send it across)
Ahh, my apologies if that wasn't made clear enough, In this instance I suppose it'd be the axis that goes directly up and down. This is purely going of the global "Y-Axis" cube usually found over in the Scene view.
Though for that little matter, that's an adorable stick figure you've managed to draw there
Answer by jimjames · Apr 14, 2014 at 04:12 AM
This worked for me for rotating objects on the y axis only. Not sure how to help you on the other parts. Sorry I am new too.
#pragma strict
var damping : int=200;
var target : Transform;
function LateUpdate()
{
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
Hmm, didn't quite work as anticipated sorry, Though thank you for the script all the same, as it did admittedly make for a rather adorable little "Following Pet" AI system.
Though it's still not quite what I'm currently for unfortunately. I suspect that taking a decent glimpse at the Camera script may be the only proper way to solve this. Though I'm just not familiar enough with C Sharp unfortunately, and from what I can see the Camera script is fairly complex as is
Glad it helped. Its a great script for things like that. GL on your project.
Answer by Nirav-Madhani · Jan 12, 2015 at 06:55 AM
Something like this
var multiple : float = 1.0;
var turnSpeedMultiplier : float = 2.0;
var turnSpeed : float = 0.0;
//// in update add following code
var targetTurnSpeed : float = 0.0;
targetTurnSpeed= Input.GetAxis("Mouse X");
targetTurnSpeed *= Mathf.Pow(turnSpeedMultiplier,3);
targetTurnSpeed *= multiple ;
turnSpeed = Mathf.Lerp(turnSpeed, targetTurnSpeed, Time.deltaTime * 25.0);
transform.rotation.eulerAngles.y += turnSpeed * Time.deltaTime;
Your answer
Follow this Question
Related Questions
How to rotate a gameobject (Y Axis) via c#? 2 Answers
Player does not follow rotation direction 0 Answers
Freeze Camera Rotation that is attached to the object in certain moments 0 Answers
Camera movement question 1 Answer
Display object panel in front of camera with correct rotation and position (no parent) 0 Answers