Moving a 3d object in unity on mouse click with click to move controller


I am using a custom character controller that moves my character around a scene through mouse clicks, similar to diablo. I would like to be able to click on an object in the scene that would then disable the controller and move the object with the mouse until clicked again, at which point the object would detach from the mouse at that position and the click to move controller would be re-enabled.

any suggestions would be greatly appreciated :slight_smile:

sorry for how long this is, i don’t really know where I would alter this for the desired effect. the code was provided by some programming students in a concurrent program with mine. we are getting minimal code support for it

private var yourCharacterController : CharacterController;
private var dir : Vector3;

private var hit : RaycastHit ;

private var rot = 0;

private var k : Vector3;

var gravity = 1.0;

static var grounded : boolean = false;

//Public Variables: Adjust in front end
var RunSpeed = 10; //Character’s running speed
var WalkSpeed = 5; //Character’s walking speed
var jumpSpeed = 2; //speed of jump
var characterDefense: float = 0; //character’s defense stat
var marker : GameObject; //what is used to show walk destination
var targetDist: int = 1; //how close to target character needs to be to stop walking

//For multiple Idle States
public var bonusIdles : int = 0; //the number of additional idle animations / sounds set in front end.
static var idleArray = new Array(); //will hold additional idle animations
static var idleSounds = new Array(); //will hold additional idle sounds
private var idlePlay:String = “idle”;
private var idleTime : int = 0; //amount of time we have been stopped

//Private Variables
private var going : boolean = false; //whether the player is moving or not
private var running: boolean = false; //if running is false, they player will be walking
private var speed: int; //actual speed player will move
private var moveAni : String = “walk”; //the move animation to be used (walk or run)
private var xDist: float; //distance from target destination
private var zDist: float; //distance from target destination
static var goTo : Vector3; //current target destination

//Static Variables
static var interacting: boolean = false; //if character is interacting with object/enemy or free to move
static var mouseOverGUI: boolean = false; //if mouse is over a GUI button

static var clickTimer: int = 0;
static var spaceTimer : int = 0;
static var jumping : boolean = false;

static var defense: float;

function Awake () {//Don’t destroy the player between scenes
DontDestroyOnLoad (transform.gameObject);

function Start() {

yourCharacterController = GetComponent(CharacterController);

yourCharacterController.isTrigger = true;

speed = WalkSpeed; //start out with walk speed
defense = characterDefense; //start out with normal defense

animation.wrapMode = WrapMode.Loop;
//for our default setting, we’ll set all our animations to loop

animation["run"].layer= -1;
animation["walk"].layer= -1;
animation["idle"].layer= -1;

//animation["idle1"].layer= -1;
//animation["idle2"].layer= -1;
//animation["idle3"].layer= -1;
//animation["idle4"].layer= -1;
//animation["idle5"].layer= -1;

//puts these animations all on the same layer

animation["jump"].layer= 10;
animation["jump"].wrapMode = WrapMode.Once; 
//makes it so our jump animation doesn't loop while we're in the air
animation["dead"].layer= 10;
animation["dead"].wrapMode = WrapMode.Once; 

	//Name of your melee attack animation in quotes
	animation["melee"].layer= 10;
	animation["melee"].wrapMode = WrapMode.Once; 
	//Name of your ranged attack animation here
	animation["ranged"].layer= 10;
	animation["ranged"].wrapMode = WrapMode.Once; 
	//Name of your defensive animation here
	animation["defend"].layer= 10;
	animation["defend"].wrapMode = WrapMode.Once; 
	//Name of your interaction animation here
	animation["interact"].layer= 10;
	animation["interact"].wrapMode = WrapMode.Once;
----------------------------------------------- */


	animation.Play("idle"); //starts off playing our idle animation by default

//Generate arrays for additional idle animations and sounds
for(i=0;i100){ //if we've been idle for a while
		idleAnim(); //play new idle animation

if (running){
	speed = RunSpeed;
	moveAni = "run";
	speed = WalkSpeed;
	moveAni = "walk";

if(Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)){//when shift button is pressed
	if(running){ //if already running, change to walking
		running = false;
	}else{ //if already walking, change to running
		running = true;

if(Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)){//when control button is pressed	
//find x and z distances between player and target
xDist = Mathf.Abs(transform.position.x-goTo.x);
zDist = Mathf.Abs(transform.position.z-goTo.z);

if(xDist <= targetDist && zDist <= targetDist){ //how close the character needs to be to the target destination to stop moving
	going = false;
		delete = GameObject.Find("MARKER(Clone)");
		Destroy(delete); //delete the destination marker now that we're there


function Update(){
yourCharacterController.Move (Vector3(0, -gravity, 0));

if(Input.GetButton("Jump")){ //if we press the jump button (spacebar)
 	if(jumping == false){	
 		if(spaceTimer <15){
	if(spaceTimer <20){
		animation.CrossFade("jump"); //blends from the current animation to the jump animation
		yourCharacterController.Move (Vector3(0, 2, 0)); //move character up
		JumpButton.jumping = false;
	spaceTimer = 0;


function jump(){
if(Pause.paused == false){
animation.CrossFade(“jump”); //blends from the current animation to the jump animation
yourCharacterController.Move (Vector3(0, jumpSpeed, 0)); //move character up
//var thisChar : GameObject = GameObject.Find(“Player”);
//thisChar.AddForce (0, 10, 0);



function defend(){
defense = (characterDefense + WeaponUI.weaponDefense);//raise defense by weapon stat
//defense += WeaponUI.weaponDefense;
print("defending, defense= " + defense);

yield WaitForSeconds(5); //wait 5 seconds
defense = characterDefense;
print("not defending, defense= " +defense);


function interact(){

function idleAnim(){
// animation.CrossFade(“jump”);
idleTime = 0;

if(bonusIdles > 0){
	var rand = Random.Range(0,bonusIdles); //pick random number	
	if (rand == 0){
	}else if (rand == 1){
	}else if (rand == 2){
	}else if (rand == 3){
	}else if (rand <3){


function doIdle(num: String){
//SFX = AudioMngr.GetComponent(AudioScript);
yield WaitForSeconds (2);
animation.CrossFade(“idle”);//}//blend to the idle animation

function OnTriggerEnter(other : Collider){
grounded = true;
//print(“collision detected”);

function OnTriggerStay(other : Collider){
grounded = true;
//print(“collision detected”);

function OnTriggerExit(other : Collider){
grounded = false;
// print(“no collision atm”);

@script RequireComponent(CharacterController) //include the character controller when this script is applied