- Home /
Question by
boomtom · Oct 14, 2020 at 02:11 AM ·
c#distancelocalpositionlocal axis
Local distance movement
I have been working on making a boxing game, and for the base I am having a capsule with a cube on either side of it as the fists. I have the player moving and rotating with the camera, but the fists will not move in the correct direction or distance. Here is my movement code:
public class PlayerMovement : MonoBehaviour
{
public float speed = 10f;
public float horizontalSpeed = 2.0F;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
float h = horizontalSpeed * Input.GetAxis("Mouse X");
transform.Rotate(0, h, 0);
}
private void Movement()
{
if (Input.GetKey("w"))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey("s"))
{
transform.Translate(Vector3.forward * -speed * Time.deltaTime);
}
if (Input.GetKey("a"))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey("d"))
{
transform.Translate(Vector3.left * -speed * Time.deltaTime);
}
}
}
Here is my punch script:
public class Punch : MonoBehaviour
{
public Transform rFist;
public Transform lFist;
public Transform player;
Vector3 startingPosL;
Vector3 startingPosR;
Vector3 endPositionL;
Vector3 endPositionR;
public float punchSpeed;
//maybe map mesh to these colliders to be controlled by them
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
lFist.rotation = player.rotation;
rFist.rotation = player.rotation;
endPositionL = new Vector3(player.position.x + 1, player.position.y, player.position.z + 1);
endPositionR = new Vector3(player.position.x - 1, player.position.y, player.position.z + 1);
startingPosL = new Vector3(lFist.position.x, lFist.position.y, lFist.position.z);
startingPosR = new Vector3(rFist.position.x, rFist.position.y, rFist.position.z);
Punching();
}
private void Punching()
{
if (Input.GetMouseButton(0))
{
if (lFist.position != endPositionL)
{
lFist.position = Vector3.MoveTowards(startingPosL, endPositionL, punchSpeed * Time.deltaTime);
}
}
if (Input.GetMouseButton(1))
{
if (rFist.position != endPositionR)
{
rFist.position = Vector3.MoveTowards(startingPosR, endPositionR, punchSpeed * Time.deltaTime);
}
}
}
}
At first I parented the fists to the player, but that did not work so I am now seeking help.
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Creating a 2d boundary 1 Answer
Need help with Tricky problem with Distance calculation 0 Answers