Hello guys!
I'm really new at writing scripts as well as unity, but I'm trying to do something very basic. I've created a character and created bones etc. in the Sprite Editor. I've written a script for the arms that go to the curser on a click. However, I'm now trying to make the rest of the body move when the cursor is for example too high. So I tried to start by writing a script I want to connect to the pelvis bone that whenever the Y position of the cursor is higher than a given value, jumps upward with a given value. I included the script below.
public class Hipmovement : MonoBehaviour
{
public Vector3 startPosition;
public Vector3 targetPosition;
public Vector3 newPosition;
public float newYPosition;
public Vector3 hipMoveY;
private bool move;
// Start is called before the first frame update
void Start()
{
startPosition = transform.position;
}
public void TargetHipLocation()
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (targetPosition.y >= 6)
{
newYPosition = startPosition.y + 3;
newPosition = new Vector3(startPosition.x, newYPosition, startPosition.z);
move = true;
}
}
public void moveHip()
{
transform.position = Vector3.MoveTowards(startPosition, newPosition, Time.deltaTime);
move = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
TargetHipLocation();
}
if (move)
{
moveHip();
}
}
}
However, when I run the game, the Y - position of the bone in the transform tab shows to be 6.2, but the Y - position from "transform.position" in the script as startPosition is -0.8900. The values simply do not match.
Can anyone help me out? Thank you!
Cheers, Nino
↧