While (starting jumping or in jump) holding W key makes game object jump high and fall slow.
This is my own upgrade to Roll-a-Ball game. So while starting jumping or already in jump pressing the W key(moving backwards relative to camera) makes game object jump higher and fall really slow (like with decreased gravity) than when jumping with A W or D keys pressed. (Also while pressing W key game object jumps not so high as when A or D are pressed, but it isn't a main problem) PlayerController script:
public float speed;
public Text countText;
public Text winText;
public float force;
private AudioSource au;
private int count;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
au = GetComponent<AudioSource>();
count = 49;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f, moveVertical);
movement = Camera.main.transform.TransformDirection(movement);
if (Input.GetKeyDown(KeyCode.Space))
{
if (rb.velocity.y < 0.001 && rb.velocity.y > -0.001 && count > 49)
{
rb.AddForce(new Vector3(0.0f, force, 0.0f));
au.Play(0);
}
}
rb.AddForce(movement * speed);
}
This started after adding the movement = Camera.main.transform.TransformDirection(movement);
line, but I need it to move the Player relative to camera.
How can I improve it, how does this line even affect my game object this way?
Answer by Euge · Apr 19, 2016 at 03:27 PM
Ok, I understood the essence of the problem. Here's my cam script
public GameObject player;
public float speed;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
transform.position = player.transform.position + offset;
transform.RotateAround(player.transform.position, Vector3.up, Input.GetAxis("Mouse X") * speed);
transform.LookAt(player.transform);
offset = transform.position - player.transform.position;
}
The thing is that my camera always points at the Player from above at a certain angle. the "movement = Camera.main.transform.TransformDirection(movement);" pushes the ball to the ground when moving forward (when pressing W the force is applied to the ball pointing from camera to the ball center) and tries to lift player when pushing S key.
Now I'm thinking on a solution. Is there any way to make TransformDirection work in X-Z plane so i don't get these vertical forces?
Gosh, silly of me. Just need to put
movement = new Vector3(movement.x, 0.0f, movement.z);
before
rb.AddForce(movement * speed);
Works like a charm. Just needed that good night sleep. ##Topic closed.
Your answer
Follow this Question
Related Questions
Jump Using Raycast. 0 Answers
how can make my player to jump like parabola 1 Answer
My jump scripit wont work. i have been tryingtt o make it work for an eternity. please help. 1 Answer
how do I make my character jump while running without losing all your speed 1 Answer
Player looses ability to jump further right player moves. 1 Answer