Question by
Hopp4973 · Sep 16, 2015 at 03:36 AM ·
arrow-keys
Arrow keys move player in opposite direction (Camera)
Hey there! I'm making a very simple game where a sphere jumps when I press "space" and the arrow keys move the sphere left and right.
However, when I press the left arrow key the player goes right and vice
How do I get the arrow keys to function as they should? I think its a Camera error- but I'll place my coding for the player in just in case.
here is my code for my player- the sphere;
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float jumpSpeed;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("space"))
{
print ("key pressed");
MakeMeJump();
}
}
void MakeMeJump()
{
rb.AddForce (Vector3.up * jumpSpeed, ForceMode.Force);
}
// Update is called once per frame
void FixedUpdate () {
float horizontalMovement = Input.GetAxis("Horizontal");
float verticalMovement = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (horizontalMovement, 0.0f, verticalMovement);
rb.AddForce (movement * speed);
Debug.Log (horizontalMovement);
Debug.Log (verticalMovement);
}
}
Comment
Answer by LiamSorta · Sep 16, 2015 at 12:03 PM
You should be able to solve this by adding a '-', this essentially reverses the Vector.
rb.AddForce (-movement * speed);
Take these for example:
Vector3 positive = new Vector3(4, 4, 4);
Vector3 negative = new Vector3(-3, -3, -3);
Calling -positive would give (-4, -4, -4) and -negative would give (3, 3, 3).
Your answer
![](https://koobas.hobune.stream/wayback/20220612033918im_/https://answers.unity.com/themes/thub/images/avi.jpg)