transform prefab on creation

Hi all

This is bugging me no end. I can see the prefab being instantiated and being transformed while I click and drag, but then it reverts to zero. How can I stop it at the scale I want? This is the code so far. It is on the prefab itself.

var newScale : float;

    function OnMouseDown () {
 	while (Input.GetMouseButton(0))
    {	 
	  newScale = Input.GetAxis("Mouse X") * 100;
	  yield;
	}
  }
   function LateUpdate () {  
      transform.localScale.x = newScale;
   }

I think you want to say:

 newScale += Input.GetAxis("Mouse X") * 100;

Cool

Thank you!

Who knew, just a little plus would do the trick… NOT me.

Cheers

I get so angry when its such a little thing like that. Can drive you insane for hours…even days.

Okay, so I have this working now… but not quite as I would like.

The scale of the object tends to flip, so if I click and drag to the right it’s fine, but drag to the left and it flips the object. The object of this exercise is to scale and rotate the object (which is an arrow) at the same time. The arrow point should point towards the mouse cursor, and the further away I drag from the first click (the arrow’s origin) the larger the arrow should become.

I tried to look on the forums for some code to calculate distance between a click an mouse position, because that distance (in whatever direction) can then control the scale. Any ideas?

var newScale : float;

    function OnMouseDown () {
 	while (Input.GetMouseButton(0))
    {	 
	  newScale += (Input.GetAxis("Mouse X")/2);
	  
	  var Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
      var hit: RaycastHit; 
      Physics.Raycast( Ray,hit,Mathf.Infinity ); 
      transform.LookAt(hit.point); 
      	  
	  yield;
	}
  }
   function LateUpdate () {  
      transform.localScale.x = newScale;
      transform.localScale.z = newScale;      
   }

That was easier than I though…
newScale = Vector3.Distance(transform.position, hit.point)/5;

Now all I need is for the correct axis on the arrow to point at the cursor. At the moment the side of the arrow is pointing at the cursor, not the pointy bit… :?

var newScale : float;

    function OnMouseDown () {
 	while (Input.GetMouseButton(0))
    {	 
	  	  
	  var Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
      var hit: RaycastHit; 
      Physics.Raycast( Ray,hit,Mathf.Infinity ); 
      transform.LookAt(hit.point); 
      newScale = Vector3.Distance(transform.position, hit.point)/5;
      	  
	  yield;
	}
  }
   function LateUpdate () {  
      transform.localScale.x = newScale;
      transform.localScale.z = newScale;      
   }

When you use transform.lookat, you can pass a second argument to state the upward direction, change that direction to match so that you arrow correctly.

transform.LookAt(hit.point, Vector3.forward);

something like that, I don’t know which axis you have to insert, it depends on your model.

Thanks Peter G

Just one more strange behaviour to sort out. The arrow is rotating in the desired direction, towards the mouse cursor, and scales according to how far away from the origin I drag. But if I keep the mouse button down the arrow starts pointing upwards (towards the screen) as if the cursor is above it.

code is still the same. I just changed the pivot of the arrow model in maya and imported it in again.

Here, I’m going to direct you to another script that does everything your’s does minus the scaling.

Fantastic… Now I have this code below working, and all is fine and dandy… except…

My arrows don’t need to be on a flat plane. Imagine a rough terrain and the arrows show troop movements etc. If the troops need to move up the mountain, the arrow start hitPoint needs to be in the valley and end hit point up the slope… Is there a way to specify that the playerPlane, is not a plane, but my collider connected to the object?

var newScale : float;
var speed = 4.0;

    function OnMouseDown () {
 	while (Input.GetMouseButton(0))
    {	 
	  var playerPlane = new Plane(Vector3.up, transform.position);	  
	  var Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
	  var hitdist = 0.0;
	  
	  if (playerPlane.Raycast (Ray, hitdist)) {
        var targetPoint = Ray.GetPoint(hitdist);
            
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);  
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    }

      newScale = Vector3.Distance(transform.position, targetPoint)/10;
      	  
	  yield;
	  }
    }

   function LateUpdate () {  
      transform.localScale.x = newScale;
      transform.localScale.z = transform.localScale.x;     
   }

Oh, sorry, then the script I gave you won’t help much. :sweat_smile:.

Well…back to your previous script.

By gum! I think I’ve done it!

    var newScale : float; 

    function OnMouseDown () { 
    while (Input.GetMouseButton(0)) 
    {    
          
      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      var hit : RaycastHit; 
      Physics.Raycast (ray, hit); 
      if(hit.collider.gameObject.name.Contains("colider")) {
      transform.LookAt(hit.point); 
      }
      newScale = Vector3.Distance(transform.position, hit.point)/10; 
           
     yield; 
   } 
  }
 
   function LateUpdate () {  
      transform.localScale.x = newScale; 
      transform.localScale.z = newScale;      
   }

Just one last question…

I looked, but couldn’t find an answer.

I would like the hit.point to be offset from the surface, so it’s just above it. This way the arrow will be completely visible.

I can’t find anything to offset it.

Thanks

Have the arrow aim at this

 function OnMouseDown () { 
    while (Input.GetMouseButton(0)) 
    {    
        var aimPoint : Vector3 = hit.point + Vector3.up;
   } 
  }

Tried it, but the result is still the same…
Can I not offset it by a number? say 10 or 15?
Just don’t know where to pass on the int or what to add it to.

Stumped with this version of my code. It’s a bit different because I can move the instance around the screen… The problem is with the “while” statement because I need to perform two things separately while the mouse button is down.

  1. Move the instance.
  2. Scale the instance when an additional key is pressed.

At the moment (1) is working and (2) is half working, because as well as scaling it also performs the movement. I tried…
[code}while (Input.GetMouseButton(0) Input.GetKey(“backspace”)){[/code]
I tried “if” and “else”, but ended up crashing Unity all over the shop. I’m sure it’s really simple logic that I’m missing.

var newScale : float; 

function OnMouseDown () {
	if (Input.GetKey(KeyCode.LeftAlt)) 
        {	        
        }
    else
        {
  	var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
	var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
	while (Input.GetMouseButton(0)){        
		var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
		var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
		transform.position = curPosition;
	if (Input.GetKey("backspace")){
		newScale += (Input.GetAxis("Mouse X"));	
		transform.localScale.x = newScale; 
        transform.localScale.z = newScale;
	}
		yield;
	}
  }
}

:sweat_smile: I think I solved it… Sorry… :slapface:

var newScale : float; 
var mouseOverColor = Color.blue;
private var originalColor : Color;

function Start () {
	originalColor = renderer.sharedMaterial.color;
}
function OnMouseEnter () {
	renderer.material.color = mouseOverColor;
}

function OnMouseExit () {
	renderer.material.color = originalColor;
}

function OnMouseDown () {
	if (Input.GetKey(KeyCode.LeftAlt)) 
        {	        
        }
    else
        {
  	var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
	var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
	while (Input.GetMouseButton(0)){
		if (Input.GetKey("backspace")){
		newScale += (Input.GetAxis("Mouse X"));	
		transform.localScale.x = newScale; 
        transform.localScale.z = newScale;
	}
	else 
	{       
		var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
		var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
		transform.position = curPosition;
	}
		yield;
	}
  }
}