- Home /
Change to constantly move forward
Hello, I have this piece of code and after coming back to my scripting days I have wanted to change it, I would really for the character to constantly move forward. I'm really sorry, could anyone help me with this? Code below :) This movement script is for moving around a planet (a sphere with its own gravity) using UnityEngine; using System.Collections;
[RequireComponent (typeof (GravityBody))]
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 1;
public float mouseSensitivityY = 1;
public float walkSpeed = 6;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
void Awake() {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
// Grounded check
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
grounded = true;
}
else {
grounded = false;
}
}
void FixedUpdate() {
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
}
GetComponent().velocity = transform.forward * moveAmount;
i would use it ins$$anonymous$$d of :
moveAmount = Vector3.SmoothDamp(moveAmount,target$$anonymous$$oveAmount,ref smooth$$anonymous$$oveVelocity,.15f);
$$anonymous$$aybe it can help!
Answer by Diukrone · Jan 13, 2018 at 01:26 PM
You should use AddForce.
@BadwolfX sorry mate, i was wrong. You need to call the Rigidbody component from where you attached your script and to change it´s velocity by using the Velocity class from Rigidbody!.
In the way you are doing, i know this way very well. $$anonymous$$ovePosition will teleport your object, not really to move it. Avoid to use SmoothDamp to velocity and use Lerp to keep a constant velocity data.
I would use SmoothDamp to Axis Input values and Lerp to constant velocity. Ah yeah, all your rigidbody calculations (Vector3 and Quaternion) need to be in Fixed Updated. On Update just put things that need to start and on LateUpdate only let the less used commands in game, as: loading animations, trigger events, pressing a key, changing câmera positions, etc.
You need to rewrite your script and to put functions and class in the right parts of the code. You are almost there.
On FixedUpdate, you can try all your SmoothDamp values on this code, on FixedUpdate:
myPrefab.AddForce(Vector3.up * gravityStability);
myPrefab.rotation = Quaternion.Euler(new Vector3(smoothX, lerpY + smoothY, smoothZ));
Remember that you can substitue the smooth values by random numbers just to test the code. There where i Lerp you put your velocity calculation value using Lerp and it will Works also with smoothed Y axis inputs or whatever.
Quaternion.Euler(new Vector3(0, 0, 0);
You use NEW VECTOR3 just to create smooth while moving it.
If do not to have the new Vector3, the values will change instantly the prefab. Eveything that will change the transform position need to be smoothed with new Vector3.
Your answer
Follow this Question
Related Questions
Character rotates left and right instead of going forward. 1 Answer
360 Degree Snake 0 Answers
Dodge movement - restrict movement of an object left and right 0 Answers
Question about moving an object technique 3 Answers
Moving tram along rail. 0 Answers