i made a script for aiming down the sight but it won’t move the object, how do i do it? I keep getting this error? here’s my script:
using UnityEngine;
using System.Collections;
public class AimingDownSight : MonoBehaviour {
private Vector3 newPosition;
void Awake()
{
newPosition = transform.position;
}
void Update ()
{
positionChanging();
}
void positionChanging()
{
Vector3 positionA = new Vector3 (0.3946627f, -0.1788931f, 0.7112392f);
Vector3 positionB = new Vector3(0.009421766f, -0.1324487f, 0.5510964f);
if (Input.GetMouseButtonDown(1))
{
newPosition = positionB;
transform.position(new Vector3(0.009421766f, -0.1324487f, 0.5510964f));
}
else
{
newPosition = positionA;
transform.position(new Vector3(0.3946627f, -0.1788931f, 0.7112392f));
}
transform.position = Vector3.Lerp (transform.position, newPosition, Time.deltaTime);
}
}
When posting code with an error, please copy the entire error message from the console and post it with your question. Your issues appear to be lines 24 and 30. ‘transform.position’ is not a method, so you cannot deal with it this way. In addition, if I’m guessing correctly about what you want this code to do, you don’t want either line in your script. Also Input.GetMouseButtonDown() only returns true for a single frame. I think you want Input.GetMouseButton(). Here is your code with these two fixes, plus I added a ‘speed’ variable. If you don’t want eased motion at the ends, you can Vector3.MoveTowards() instead of Lerp().
using UnityEngine;
using System.Collections;
public class AimingDownSight : MonoBehaviour {
public float speed = 5.0f;
private Vector3 newPosition;
void Awake()
{
newPosition = transform.position;
}
void Update ()
{
positionChanging();
}
void positionChanging()
{
Vector3 positionA = new Vector3 (0.3946627f, -0.1788931f, 0.7112392f);
Vector3 positionB = new Vector3(0.009421766f, -0.1324487f, 0.5510964f);
if (Input.GetMouseButton(1))
{
newPosition = positionB;
//transform.position(new Vector3(0.009421766f, -0.1324487f, 0.5510964f));
}
else
{
newPosition = positionA;
//transform.position(new Vector3(0.3946627f, -0.1788931f, 0.7112392f));
}
transform.position = Vector3.Lerp (transform.position, newPosition, Time.deltaTime * speed);
}
}