- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Alp-Giray-Savrum · Mar 10, 2015 at 08:09 PM ·
movementmouseparentaxis
I can'^t move object in seceted axis
Hi guys, I've written a code piece, It Works actually but it isn't Works as i wanted.
///////////////////////////////////////////////////////////////////////////////
//This script is written by Alp Giray Savrum
// SmoothMoveToMousePoint.js ; 10 March 2015, Monday 21.29
///////////////////////////////////////////////////////////////////////////////
//Smooth Multiplier which is like SmoothFollow.js
var smoothMultiplier : float;
//Hit distance determines, where will our ray is going to stop ("Z" axis for directly forward),
var hitDistance : int;
//Our clicked target position holder. Also works for observation purposes.
var clickedTargetPosition:Vector3;
//Our real target position holder. Works for observation purposes too.
var targetPosition : Vector3;
//Is object going to be move in "x" axis ?
var xAxis = false ;
//Or will it move in "y" axis ?
var yAxis = false;
//Update function.
function Update () {
//Check for is player clicking left mouse ? (Default Mouse0 is Left, also works for touch at mobile devices)
/* GetKey, instead of GetKeyDown works for continuous input. */
if(Input.GetKey(KeyCode.Mouse0))
{
//Create a ray variable from camera point to screen click point (universe).
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//Get clicked position's universal positions.
clickedTargetPosition = ray.GetPoint(hitDistance);
//If object will move in "x" axis...
if(xAxis) {
//Change only "x" axis.
targetPosition.x = clickedTargetPosition.x;
}
//If object will move in "y" axis...
if(yAxis) {
//Change only "y" axis.
targetPosition.y = clickedTargetPosition.y;
}
}
//Change position smoothly to targetPosition.
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smoothMultiplier);
}
At this code, I've programmed object to move in x axis when i click. It runs nice but, when i start to move camera (Parent object, has it's own movement script) object stucks at it's default place. I don't know why.
Comment
Your answer
Follow this Question
Related Questions
How to control an object with a mouse? 1 Answer
Huh click and go script working 50% 0 Answers
Help with 2D AI scripting 2 Answers
moving an object with mouse ( my script is not smooth enough ) 0 Answers