- Home /
Duplicate Question
need help on this script
Hi All,
I making a small app to find the length of the small objects and radius. I just want to move my vernier calipers mover object. How will i write script for the moving of the object. can i get any code or any idea to do that, then i will implement.
using UnityEngine;
using System.Collections;
public class XDrag : MonoBehaviour {
public float sensitivity = 0.02f;
private Vector3 v3Prev;
void Update () {
if (Input.GetMouseButtonDown(0)) {
v3Prev.x = Input.mousePosition.x;
}
if (Input.GetMouseButton (0)) {
transform.position.x += (Input.mousePosition.x - v3Prev.x) * sensitivity;
v3Prev.x = Input.mousePosition.x;
}
}
}
After writing my script i am getting an error like this:
Assets/Scripts/XDrag.cs(14,15): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Can i get any help.
Thanks
Shankar
Try replacing transform.position.x += ..... with transform.localEulerAngles.x += .....
Answer by AlucardJay · Apr 29, 2013 at 06:09 PM
In C# you cannot modify just one value of the transform vector variable. The answer is in the error message : Consider storing the value in a temporary variable
Vector3 newPos = transform.position;
newPos.x = newPos.x + (Input.mousePosition.x - v3Prev.x) * sensitivity;
transform.position = newPos;
@alu just close and delete more questions to save me having to do it dude
It's already been answered, so just click on the grey tick to accept it (if it fixed your problem). You could change the title to something more suitable like Cannot modify a value type return value of UnityEngine.Transform.position for future people searching the problem.
Thanx @alucardj. The vernier scale is moving in x-axis but the problem is that it should only move when I drag the scale, but its also moving on the empty screen background if i drag my mouse in empty screen.
using UnityEngine;
using System.Collections;
public class XDrag : $$anonymous$$onoBehaviour {
public float sensitivity = 0.02f;
private Vector3 v3Prev;
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
v3Prev.x = Input.mousePosition.x;
}
if (Input.Get$$anonymous$$ouseButton (0)) {
Vector3 newPos = transform.position;
newPos.x = newPos.x + (Input.mousePosition.x - v3Prev.x) * sensitivity;
transform.position = newPos;
v3Prev.x = Input.mousePosition.x;
}
}
}
I have used this script for the vernier scale.
can please help me on that.
You need to check if you actually click on the calipers first. I would suggest a raycast on Get$$anonymous$$ouseButtonDown, if it hit the calipers, set a boolean to true, then in Get$$anonymous$$ouseButton check that boolean is true first, if so then do all the other stuff.
Follow this Question
Related Questions
How to realize the real world physics? 1 Answer
Making an object fly at selected item. 0 Answers
2.5D Side-scroller Mouse Drag Objects 1 Answer