- Home /
Question by
mairadumb · Nov 08, 2020 at 08:50 AM ·
rotationclampobject referenceclamped rotation
How to clamp rotation of an object?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PLayermoves : MonoBehaviour
{
public Rigidbody rg;
public float forwarforce = 0.000001f;
public float sidewaysforce = 0.00001f;
public float upwardsforce = 0.000005f;
// Update is called once per frame
void FixedUpdate()
{
rg.useGravity = true;
if (Input.GetKey("w"))
{
rg.AddForce(0 , 0, forwarforce * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rg.AddForce(sidewaysforce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rg.AddForce(-sidewaysforce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("s"))
{
rg.AddForce(0, 0, -forwarforce * Time.deltaTime);
}
if (Input.GetKey("space"))
{
rg.AddForce(0, upwardsforce * Time.deltaTime, 0 );
}
}
}
Comment
Answer by mairadumb · Nov 08, 2020 at 08:54 AM
Actually my object that this script is attached falls down when he walks.So,please tell what can i add to stop it's rotation.
Answer by CodesCove · Nov 08, 2020 at 04:26 PM
From inspector find the Rigidbody component and there Constraints --> Freeze Rotation. Check the X and Z axis to prevent is from falling and Y axis to prevent it from turning (around Y axis),
Your answer
Follow this Question
Related Questions
How to use mathf.clamp? 2 Answers
How to Clamp a Quaternion Camera Rotation? 0 Answers
Clamped rotation of turret problem 0 Answers
Bizzare problem with limiting camera veritcal rotation 2 Answers
Clamping Vector3 2 Answers