Script preventing gravity from working.
Hello! First time user here. So, I have no idea what I'm doing, but we've all been there at one point right? For some reason, My character isn't affected by gravity when this is active:
float xPos = transform.position.x + (Input.GetAxis("Horizontal") * moveSpeed);
playerPos = new Vector3(Mathf.Clamp(xPos, -50, 50), 0, 0);
transform.position = playerPos;
When this part of the script is gone, the Gravity suddenly works, but I can't move (as expected). In the Rigidbody, "Use Gravity" is checked and "Is Kinematic" is unchecked.
This is the entire script:
using UnityEngine;
using System.Collections;
public class IceDeeMovement : MonoBehaviour {
public float moveSpeed = 0.3f;
public Vector3 playerPos;
void Update()
{
float xPos = transform.position.x + (Input.GetAxis("Horizontal") * moveSpeed);
playerPos = new Vector3(Mathf.Clamp(xPos, -50, 50), 0, 0);
transform.position = playerPos;
}
}
Can someone tell me what's wrong?
Answer by A13x231 · Sep 18, 2016 at 04:12 PM
Every update (frame), you're changing the playerPos y to 0.
You're changing the xPos and just setting y and z to 0. Even if gravity is trying to move the player every frame you're moving them back to y 0.
changing
playerPos = new Vector3(Mathf.Clamp(xPos, -50, 50), 0, 0);
to
playerPos += new Vector3(Mathf.Clamp(xPos, -50, 50), 0, 0);
should solve the problem.
Your answer
Follow this Question
Related Questions
Movement script preventing gravity 1 Answer
Movement script preventing gravity 0 Answers
Script preventing Gravity from working 1 Answer