- Home /
Set position and rotation.
This one must be extremely easy to answer. I have an object in the scene, and I want it so clicking on it will instantly change the position and rotation of the player. So essentially I'm asking both how to instantly change the position and rotation of the player, and how to make the script effect the player instead of itself.
This is for a point-and-click style game, so I want to be able to set these "buttons" all over the scene in order to move the player around. Position and rotation being variables, of course.
Answer by psychentist · Apr 20, 2011 at 04:04 AM
well, it depends on if you know the rotations you want to set the player to, or if you want to manipulate the existing one. Let's just say that you want the right arrow to rotate your player to the right by 90 degrees. fair enough? (you can also use vector3, and Euler3, but I dont feel like looking up the syntax right now. :-P)
Locrot Manager (attach to its own empty object)
static var playerX : float; static var playerY : float; static var playerZ : float;
static var targetX : float; static var targetY : float; static var targetZ : float;
static var targetXLoc : float; static var targetYLoc : float; static var targetZLoc : float;
Player Rotator (attach to the player object)
function Update(){
locrotManager.playerX = transform.rotation.x; locrotManager.playerY = transform.rotation.y; locrotManager.playerZ = transform.rotation.z; }
function Rotate(){
transform.rotation.x = locrotManager.targetX; transform.rotation.y = locrotManager.targetY; transform.rotation.z = locrotManager.targetZ; }
function Move(){
transform.position.x = locrotManager.targetXLoc; transform.position.y = locrotManager.targetYLoc; transform.position.z = locrotManager.targetZLoc; }
Right Arrow (attach to the arrow/button thingy)
function OnMouseDown(){
locrotManager.targetX = locrotManager.playerX; locrotManager.targetZ = locrotManager.playerZ;
if (locrotManager.playerY >= 270){ locrotManager.targetY = locrotManager.playerY - 270; }
else { locrotManager.targetY = locrotManager.playerY + 90; }
PlayerRotator.Rotate(); }
Forward Arrow (attach to the arrow/button thingy)
var targetX : float; var targetY : float; var targetZ : float;
function OnMouseDown(){
locrotManager.targetXLoc = targetX; locrotManager.targetYLoc = targetY; locrotManager.targetZLoc = targetZ;
playerRotator.Move(); }
according to what you were saying, the arrows will be all over the scene, right? if so, then you should be able to figure out where you want each one to move them to and set the target coordinates in the editor, so each arrow can have the same script.
Answer by Jean-Fabre · Apr 20, 2011 at 08:44 AM
Hi,
You are speaking about buttons in the scenes, if these buttons are actually representing the spot, then you'd build a script attached to each spot and when the user click on it, it position and rotate the player to match itself.
The script below will just do that.
If you want to move the player to a different location then that spot, you can uncheck useTransform and set position and rotation.
public var player:Transform; public var useTransform:boolean = true; public var position:Vector3; public var rotation:Vector3;
function OnMouseUp () {
if (player==null){
return;
}
if(useTransform){
player.position = transform.position;
player.rotation = transform.rotation;
}else{
player.position = position;
player.rotation = Quaternion.EulerAngles(rotation);
}
}
you can create as much dummies as you want, attach this script to them, set its public variables properly, and you'll be able to move your player around from spot to spot as you click on them.
Hope it helps,
Bye,
Jean
Your answer
![](https://koobas.hobune.stream/wayback/20220613123932im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to set a specific rotation? 1 Answer
Objects not always Spawning At Correct Location 2 Answers
Prefab Character position/rotation resets 0 Answers
Freeze camera 1 Answer