Hi it’s my first time to this forum and I wanna share a couple of changes I made so that the rocket manipulation can feel nicer, perhaps? One approach is to add acceleration to throttle and both acceleration and deceleration to rotation. I also used raycast to make player not only touching landing spot but also stopping quite well to pass the level.
Here’s my code. What do you guys think? Feel free to copy and try them out.
private void TrottleInput()
{
if (Input.GetKey(KeyCode.Space) && ySpeed <= maxSpeed)
{
// trottle
//myRigidBody.AddRelativeForce(Vector3.up);
//// v2
ySpeed = transform.InverseTransformDirection(myRigidBody.velocity).y + (acceleration * Time.deltaTime);
myRigidBody.AddRelativeForce(Vector3.up * (acceleration * Time.deltaTime));
//// try change velocity directly
//Vector3 velocityOnOwnAxis = transform.InverseTransformDirection(myRigidBody.velocity);
//ySpeed = velocityOnOwnAxis.y + acceleration * Time.deltaTime;
//velocityOnOwnAxis.y = ySpeed;
//myRigidBody.velocity = transform.TransformDirection(velocityOnOwnAxis);
}
else
{
// update speed
ySpeed = transform.InverseTransformDirection(myRigidBody.velocity).y;
}
}
private void Rotation()
{
// remove rotation due to physics
myRigidBody.angularVelocity = Vector3.zero;
//rotate
if (Input.GetKey(KeyCode.A) && curRotationSpeed <= maxRotationSpeed)
{
//rotate left
curRotationSpeed += rotationAcceleration * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D) && curRotationSpeed >= -maxRotationSpeed)
{
// rotate right
curRotationSpeed -= rotationAcceleration * Time.deltaTime;
}
else
{
if (curRotationSpeed > rotationDecceleration * Time.deltaTime)
{
curRotationSpeed -= rotationDecceleration * Time.deltaTime;
}
else if (curRotationSpeed < -rotationDecceleration * Time.deltaTime)
{
curRotationSpeed += rotationDecceleration * Time.deltaTime;
}
else
{
curRotationSpeed = 0f;
}
}
transform.Rotate(Vector3.forward * curRotationSpeed);
}
public bool IsLanded()
{
int landingLayerMask = LayerMask.GetMask("Landing");
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, landingDetectRange, landingLayerMask)
&& Mathf.Abs(ySpeed)<0.001)
{
return true;
}
else
{
return false;
}
}