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 lawrence-parry · Sep 19, 2016 at 01:28 AM
Since you are setting the position gravity is not affecting the object. Instead of setting the position use transform.Translate() instead.
transform.Translate(Vector3.right*Input.GetAxis("Horizontal") * moveSpeed);
Your answer
![](https://koobas.hobune.stream/wayback/20220612093032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Movement script preventing gravity 1 Answer
Movement script preventing gravity 0 Answers
Script preventing gravity from working. 1 Answer
Unity3d gravity not working in movement script 0 Answers
rb.addforce question 0 Answers