addForce problem please help!
hello , i making a tutorial for fps game , like movement and jumping . im useing addForce to make jump like the tutorial but instead of jumping ith teleporting to the point and falls by gravity. note: im not useing character controller, i create an empty gameobject (like somes tutorials) with camrea as a child. and rigidbody,boxcollider,and my script attach to gameobject. i know this question has asked before ,but i did everything. please see the code ignore the rotation and movment . using UnityEngine; using System.Collections;
public class characterControllerScript : MonoBehaviour {
public float movmentSpeed = 10f;
public float mouseSensetivity = 2.5f;
public float verticalRotationRange = 60f;
public float jumpForce = 50f;
private float VerRotation = 0f;
private double gravity = 0f;
private float timer = 0f;
// Use this for initialization
void Start () {
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
//characterMovment+mouseDirection,depending the mouse facing to so the movment is.
float horSpeed = Input.GetAxis("Horizontal")*movmentSpeed;
float verSpeed = Input.GetAxis("Vertical")*movmentSpeed;
float horRotation = Input.GetAxis("Mouse X") * mouseSensetivity;
transform.Rotate(0, horRotation, 0);
VerRotation -= Input.GetAxis("Mouse Y") * mouseSensetivity;
VerRotation = Mathf.Clamp(VerRotation, -verticalRotationRange, verticalRotationRange);
GetComponentInChildren<Camera>().transform.localRotation=Quaternion.Euler(VerRotation,0,0);
// Camera.main.transform.localRotation = Quaternion.Euler(VerRotation, 0, 0);
// Debug.Log(VerRotation);
Vector3 speed = new Vector3(horSpeed, 0, verSpeed);
Rigidbody myRG = GetComponent<Rigidbody>();
speed = transform.rotation * speed;
myRG.velocity = new Vector3(speed.x,speed.y,speed.z);
//jumping
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
Answer by RobertoLangarica · Mar 15, 2016 at 09:56 PM
Try without the ForceMode.Impulse or try with another jumpForce value (a really really low one)
Also use FixedUpdate ins$$anonymous$$d of Update when you deal with physics (Explanation here: FixedUpdate)
Answer by nati12w32q · Mar 16, 2016 at 06:28 AM
i change for FixedUpdate and ith not improving anything. also i delete the Forcemode and tried with low value. ith not working ... please another solution??
FixedUpdate was for other reasons and not to make the jump work. I copy/paste only the jump code and it works by removing the impulse mode. You are forcing the rigid body velocity and that could be your problem. Try with that line commented and see what happen
well thanks a lots!! you right about the velocity. i was never paid attention for this. but now there is another problem with the moving character. if i cant use rigidbody.velocity or tranform.position because both make same problem. what can i use to make him move and jump with addForce?
You could move the character with addForce only in the x Axis, and you could truncate the velocity to a max using rigidbody.setVelocity the problem is that you were overriding the y axis velocity