I possibly am making an easy mistake on setting velocity for every fixed update frame but can’t get around of this and need help about to understand what is really going on. When my object starts to falling, it just hangs on the air for a very long time. Here’s my two-piece script attached to the object, thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p_Control : MonoBehaviour
{
private p_Module _Playa;
private Transform _mainCam;
private Vector3 _mainCamFw;
private Vector3 _Move;
void Start()
{
if (Camera.main != null){
_mainCam = Camera.main.transform;
} else {
Debug.LogWarning("No main cam found.");
}
_Playa = GetComponent<p_Module>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
_Move = Vector3.Normalize(v*Vector3.right + h*Vector3.back);
//hijacking solution
}
void FixedUpdate()
{
if (!Input.GetButton("Run")) _Move*= 0.5f;
_Playa.Move(_Move);
_Playa.Turn();
if (Input.GetButtonDown("Fire")) _Playa.Shoot();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class p_Module : MonoBehaviour
{
Rigidbody _Rigid;
[SerializeField] float _mvspd = 100f;
[SerializeField] float camRayLength = 100f;
private Vector3 playerToMouse;
int floorMask;
void Awake() {
floorMask = LayerMask.GetMask("Floor");
}
void Start()
{
_Rigid = GetComponent<Rigidbody>();
_Rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
public void Move(Vector3 _Move)
{
_Rigid.velocity = (_Move*_mvspd);
}
public void Turn()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)){
playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
_Rigid.MoveRotation(newRotation);
Debug.DrawRay(transform.position, playerToMouse, Color.green);
}
}
public void Shoot()
{
}
void Update()
{
}
}