I even set up public variables to see if was getting any input, and it isn't. I even copied and pasted the names directly from the project settings page.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class vehicle : MonoBehaviour
{
private Vector3 thrusterInput;
public float rotationSpeed = 10f;
public float rotSmoothSpeed = 10f;
private Quaternion smoothedRot = Quaternion.identity;
private Quaternion targetRot;
Body[] bodies;
public Rigidbody rigidbody;
public float thrustStrength = 10f;
// Start is called before the first frame update
void Start()
{
bodies = FindObjectsOfType<Body>();
}
// Update is called once per frame
void Update()
{
foreach(Body body in bodies)
{
float sqrDst = (body.transform.position - transform.position).sqrMagnitude;
Vector3 forceDir = (body.transform.position - transform.position).normalized;
Vector3 acceleration = forceDir * Universe.gravitationalConstant * body.rigid.mass / sqrDst;
rigidbody.AddForce(acceleration, ForceMode.Acceleration);
}
Vector3 thrustDir = transform.TransformVector(thrusterInput);
rigidbody.AddForce(thrustDir * thrustStrength, ForceMode.Acceleration);
rigidbody.MoveRotation(smoothedRot);
}
void HandleMovement()
{
float thrustInputX = Input.GetAxis("Horizontal");
float thrustInputY = Input.GetAxis("Ascent");
float thrustInputZ = Input.GetAxis("Vertical");
thrusterInput = new Vector3(thrustInputX, thrustInputY, thrustInputZ);
float yawInput = Input.GetAxis("Mouse X") * rotationSpeed;
float pitchInput = Input.GetAxis("Mouse Y") * rotationSpeed;
float rollInput = Input.GetAxis("Roll") * rotationSpeed;
Quaternion yaw = Quaternion.AngleAxis(yawInput, transform.up);
Quaternion pitch = Quaternion.AngleAxis(pitchInput, transform.right);
Quaternion roll = Quaternion.AngleAxis(rollInput, transform.forward);
targetRot = yaw * pitch * roll * targetRot;
smoothedRot = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * rotSmoothSpeed);
}
}
Sorry if the indents are all messed up, stackoverflow wasn't letting me use tab for some reason.
Your HandleMovement() is not called from anywhere. You need to put it in Update()
Related
Im making a 3d game in unity, and so I made a cs script for movement of my charecter, walking and moveing the camera works fine, however when i added the jump function, it had a delay. You could press the jump button 5 times, with no result. Then you press it again, and it jumps. I cant figure out why this does this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController characterController;
public int speed = 6;
public float gravity = 9.87f;
private float verticalspeed = 0;
private Vector3 moveDirection = Vector3.zero;
public Transform Camera;
public float Sensitivity = 2f;
public float uplimit = -50;
public float downlimit = -50;
public float jumpspeed = 5.0f;
void Update()
{
move();
cameramove();
void cameramove()
{
float horizontal = Input.GetAxis("Mouse X");
float vertical = Input.GetAxis("Mouse Y");
transform.Rotate(0, horizontal * Sensitivity, 0);
Camera.Rotate(-vertical * Sensitivity, 0, 0);
Vector3 currentRotation = Camera.localEulerAngles;
if (currentRotation.x > 180) currentRotation.x -= 360;
currentRotation.x = Mathf.Clamp(currentRotation.x, uplimit, downlimit);
Camera.localRotation = Quaternion.Euler(currentRotation);
}
void move()
{
float horizontalMove = Input.GetAxis("Horizontal");
float verticalMove = Input.GetAxis("Vertical");
if (characterController.isGrounded) verticalspeed = 0;
else verticalspeed -= gravity * Time.deltaTime;
Vector3 gravityMove = new Vector3(0, verticalspeed, 0);
Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
characterController.Move(speed * Time.deltaTime * move + gravityMove * Time.deltaTime);
if (characterController.isGrounded && Input.GetButton("Jump"))
{
moveDirection.y = jumpspeed;
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
}
}
Assuming your character has a RigidBody assigned to it, you can refer it on your script as,
public class PlayerMovement : MonoBehaviour
{
private Rigidbody myrigidbody;
void Start () {
myrigidbody = GetComponent<Rigidbody>();
}
}
Then you can pass the jumpspeed as a Vector3, and probably you might have to trigger your animation here as well.
if (characterController.isGrounded && Input.GetButton("Jump"))
{
characterController.isGrounded = false;
myrigidbody.AddForce(new Vector3(0, jumpspeed, 0));
// Trigger your animation, myanimator.SetTrigger("jump");
}
Unity character controller move documentation also contains jump function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = -9.81f;
private void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
I'm currently working on a fps game in which you can travel to asteroids and walk on them, currently I have gravity for those asteroids and I have a fps controller that works for all my other movement systems (swimming, floating) but translating it to the gravity of the asteroid is giving me a lot of trouble. I can get the controller to work perfectly on flat surfaces using Unity's gravity but not on the asteroid.
The current fps controller moves in the direction of the camera which means the character floats up when looking up, all my other fps controllers didn't work at all with the planet and this one is the closet I've gotten to getting it to work.
Is there anyway to fix this? I'm quite new to coding so any explanations would help me heaps!
Here's the code I'm using for the controller:
public Transform targetCamera;
public Rigidbody targetRigidbody;
public Transform targetBody;
public float walkSpeed = 9f;
public float runSpeed = 14f;
public float maxSpeed = 20f;
public float jumpPower = 30f;
public float extraGravity = 0;
float bodyRotationX;
float camRotationY;
Vector3 directionIntentX;
Vector3 directionIntentY;
float speed;
public bool grounded;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private const float Sensitivity = 50f;
private const float SensitivityMultiplier = 1f;
private float desiredX;
public float dragMult = 2f;
public float rotationSmoothSpeed = 10f;
void Update()
{
Look();
targetRigidbody.drag = dragMult;
}
private void FixedUpdate()
{
if (targetRigidbody.velocity.magnitude > maxSpeed)
{
targetRigidbody.velocity = targetRigidbody.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
AddForce(Vector3.forward);
if (Input.GetKey(KeyCode.S))
AddForce(Vector3.back);
if (Input.GetKey(KeyCode.A))
AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
AddForce(Vector3.right);
if (Input.GetKey(KeyCode.Space))
AddForce(Vector3.up);
if (Input.GetKey(KeyCode.LeftControl))
AddForce(Vector3.down);
}
private void AddForce(Vector3 direction)
{
float scaledForwardSwimSpeed = Time.deltaTime * forwardSwimSpeed;
targetRigidbody.AddForce(targetCamera.transform.TransformDirection(
direction * thrust) * scaledForwardSwimSpeed,
ForceMode.Acceleration);
}
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
Vector3 rotation = targetCamera.transform.localRotation.eulerAngles;
desiredX = rotation.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
targetCamera.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
}
And here's the first of two bits of code for the asteroid: GravityCtrl
public float gravity = 10f;
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<GravityCtrl>())
{
other.GetComponent<GravityCtrl>().gravity = this.GetComponent<GravityOrbit>();
}
Here's the second: GravityOrbit
public GravityOrbit gravity;
private Rigidbody rb;
public float rotationSpeed = 20f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
if (gravity)
{
Vector3 gravityUp = Vector3.zero;
gravityUp = (transform.position - gravity.transform.position).normalized;
Vector3 localUp = transform.up;
Quaternion targetrotation = Quaternion.FromToRotation(localUp, gravityUp) * transform.rotation;
transform.up = Vector3.Slerp(transform.up, gravityUp, rotationSpeed * Time.deltaTime);
rb.AddForce((-gravityUp * gravity.gravity) * rb.mass);
}
Below is the code that I am using to move an object in Unity. The rb.Velocity line makes my object float in in Gameplay mode. If I comment out the line then the object falls just fine.
Could someone explain whats happening here?
public class PlayerController : MonoBehaviour
{
public float forwardVelocity = 0F;
public float maxSpeed = 180;
public float acceleratePerSecond = 8.0F;
public float rotateSpeed = 3.0F;
private float yaw = 0.0f;
private float pitch = 0.0f;
protected Rigidbody rb;
float timeZeroToMax = 2.5F;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
acceleratePerSecond = maxSpeed / timeZeroToMax;
forwardVelocity = 0F;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
{
if (forwardVelocity< maxSpeed)
{
forwardVelocity += acceleratePerSecond * Time.deltaTime;
}
}
forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);
rb.velocity = transform.forward * forwardVelocity;
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
yaw += rotateSpeed * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
The transform.forward is a Vector3(0,0,1), that's the problem.
You are setting your y velocity to 0.
I have the basic movement and rotation working however I can not work out a way to limit the rotation up and down. I want to make it so that you cant look more than 90° up and down.
Ive tried multiple ways such as using if statments and using clamp.
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.Rotate(-rotY, 0, 0);
}
}
With this code you will be able to rotate the camera beyond 90 degrees causing it to be upside down etc
"Camera" is a built-in unity class, I would recommend renaming it to "camera".
Try this to clamp the camera's rotation:
(with your other public floats)
public float minAngle = -90;
public float maxAngle = 90;
(at the end of FixedUpdate)
Vector3 temp = camera.transform.localEulerAngles;
camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(Mathf.DeltaAngle(0, temp.x), minAngle, maxAngle), temp.y, temp.z);
Edit: changed eulerAngles to localEulerAngles
Edit 2: changed the order of the arguements of Mathf.DeltaAngle
I fixed it here. Not to sure on how it works but it works. Credit to video:https://www.youtube.com/watch?v=F5eE1YL1ZJY
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
public float minAngle = -90f;
public float maxAngle = 90f;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY -= Input.GetAxis("Mouse Y") * sensitivity;
rotY = Mathf.Clamp(rotY, minAngle, maxAngle);
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
}
}
This is my CamMouseLook script and I need it so when the player moves the mouse all the way up it doesnt turn upside down. I want him to be able to look up just not so much up that it turns the view upside down
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMouseLook : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
}
What you can do is to lock the rotation in a specific axis. For example to limit in the Y and X axis so the player can only rotate from [-60,60] degrees you can use:
using System;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 70.0f;
public float clampAngle = 60.0f;
private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update ()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
}
Now you can adapt this script to limit the rotation in the angle and the range you need