Unity C# - How to check if random moving gameobject is within boundaries - c#

I have a random moving gameobject moving in the scene with the attached code. The issue is that although there are boundary colliders with a rigid body, sometimes it passes right through them and falls.
How can I limit it that once it bumps into those colliders it does a 189 rotation and continues moving randomly?
The terrain width is 50f, the terrain Height is also 50f.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTrainingMovement : MonoBehaviour {
public float speed;
private float randomizedSpeed = 0.0f;
private float nextActionTime = -1.0f;
private Vector3 targetPosition;
private Vector3 moveVector;
private void FixedUpdate() {
if(speed > 0.0f) {
Move2();
//Move();
}
}
private void Move2() {
if(Time.fixedTime >= nextActionTime) {
// Get a threshold time
nextActionTime = UnityEngine.Random.Range(50.0f, 200.0f);
// Pick a random target position
targetPosition = TrainingArea.ChooseRandomPosition(transform.parent.position, 0f, 270f, 2f, 10f);
// Rotate towards the target position
transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up);
float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomizedSpeed;
nextActionTime = Time.fixedTime + timeToGetThere;
// Get a movement vector
moveVector = speed * transform.forward;
//moveVector = speed * new Vector3(1.0f, 0.0f, 0.0f);
}
else {
// Check -X boundary
if(transform.position.x - 1.0f <= transform.parent.gameObject.transform.position.x) {
transform.position = transform.position + new Vector3(1.0f, 0.0f, 0.0f);
moveVector.x *= -1.0f;
}
// Check +X boundary
if(transform.position.x + 1.0f >= transform.parent.gameObject.transform.position.x + TrainingArea.terrainWidth) {
transform.position = transform.position + new Vector3(-1.0f, 0.0f, 0.0f);
moveVector.x *= -1.0f;
}
// Check -Z boundary
if(transform.position.z - 1.0f <= transform.parent.gameObject.transform.position.z) {
transform.position = transform.position + new Vector3(0.0f, 0.0f, 1.0f);
moveVector.z *= -1.0f;
}
// Check +Z boundary
if(transform.position.z + 1.0f >= transform.parent.gameObject.transform.position.z + TrainingArea.terrainHeight) {
transform.position = transform.position + new Vector3(0.0f, 0.0f, -1.0f);
moveVector.z *= -1.0f;
}
// Move
transform.position = transform.position + moveVector * Time.fixedDeltaTime;
// Update nextActionTime
nextActionTime--;
}
}
}
public static Vector3 RandomPositionOnTerrain(Vector3 origin) {
//Range(30.0f, terrainWidth - 30.0f);
float randomX = origin.x + UnityEngine.Random.Range(20.0f, terrainWidth);
float randomZ = origin.z + UnityEngine.Random.Range(20.0f, terrainHeight);
return new Vector3(randomX, 30.0f, randomZ);
}

You can use OnCollisionEnter to trigger a rotation when the unit bumps into other colliders.
something like this must do it:
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag(or name) = "a tag or name"){
//Do Stuff here
}
}
Here's the detailed documentation:
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
another workaround is using NavMesh. It'll make the movements UnityEngine.AI based and pretty smooth but you'll need a whole new script.

Related

How to make a curved flight path?

How to make a bullet fly in an arc when shooting and hit the player.
As in this picture . I tried to use formulas from physics, the body is thrown at an angle to the horizon, that's what came of it . But the bullet flies away into the void
velocity = Mathf.Round(Vector3.Distance(lastpos, transform.position) / Time.deltaTime);
lastpos = transform.position;
Vector3 direction = PlayeCar.position - Vector3.zero;
float angle = Mathf.Atan2(direction.y, direction.x); // радианы
float x = velocity * Mathf.Cos(angle) + gravity * (time* time) / 2;
float y = velocity * Mathf.Sin(angle);
transform.position = new Vector2(x, y)
;
Sample orientative method could be (The script would be attached to the gun that shots, so when the code referes to transform, refers to the gun's transfom):
public void LaunchTarget()
{
Vector3 initialSpeed = transform.forward; //shot direction, gun's forward
GameObject go = GameObject.Instantiate(m_bullet);//reference to bullet prefab
//in case you need to randomize the shot speed
initialSpeed = transform.forward * Random.Range(m_minLaunchSpeed, m_maxLaunchSpeed);
//set the initial position of the bullet in the gun
go.transform.position = transform.position;
// shoot (give speed to the bullets rigidbody)
go.GetComponent<Rigidbody>().velocity = initialSpeed;
//initially disabled soas to see the bullet when shot
go.SetActive(true);
}
Hope that helps
I did it !!!! at the time of the bullet’s flight, I turn it toward the player in an intermittent manner. And after a while I delete it
void Start()
{
rigidbodyBullet = GetComponent<Rigidbody2D>();
Player = GameObject.Find("carPlayer").GetComponent<Transform>();
_distance = Vector3.Distance(Player.position, transform.position);
Invoke("DestroyArcBullet", 1.5f);
limitDistance = _distance / 1.3f;
}
void Update()
{
transform.position += transform.up * Time.deltaTime * 5f;
_distance = Vector3.Distance(Player.position, transform.position);
if (_distance < limitDistance)
{
var turn = Quaternion.Lerp(transform.rotation,
Quaternion.LookRotation(Vector3.forward, Player.position - transform.position), Time.deltaTime * 2);
rigidbodyBullet.MoveRotation(turn.eulerAngles.z);
}
if (this.transform.position.y + this.transform.position.y <= -10 || this.transform.position.y + this.transform.position.y >= 10
|| this.transform.position.x + this.transform.position.x <= -10 || this.transform.position.x + this.transform.position.x >= 10)
{
Destroy(gameObject);
}
}

Rotating Rigidbody controller

There is my script Rigidbody controller -
public float Speed = 5f;
public float JumpHeight = 2f;
public float GroundDistance = 0.2f;
public float DashDistance = 5f;
public LayerMask Ground;
private Rigidbody _body;
private Vector3 _inputs = Vector3.zero;
private bool _isGrounded = true;
private Transform _groundChecker;
void Start()
{
_body = GetComponent<Rigidbody>();
_groundChecker = transform.GetChild(0);
}
void Update()
{
_isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
_inputs = Vector3.zero;
_inputs.x = Input.GetAxis("Horizontal");
_inputs.z = Input.GetAxis("Vertical");
if (_inputs != Vector3.zero)
transform.forward = _inputs;
if (Input.GetButtonDown("Jump") && _isGrounded)
{
_body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
}
if (Input.GetButtonDown("Sprint"))
{
Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3((Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime), 0, (Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime)));
_body.AddForce(dashVelocity, ForceMode.VelocityChange);
}
}
void FixedUpdate()
{
_body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
}
What the best way to make a turn on y in the direction of the camera ? That is,the player turns to the side where the mouse is turned? Is it in fixedUpdate or update?
This is the camera script:
public float Smoothness = 0.3F;
public Vector2 Sensitivity = new Vector2(4, 4);
public Vector2 LimitX = new Vector2(-70, 80);
private Vector2 NewCoord;
public Vector2 CurrentCoord;
private Vector2 vel;
public GameManager GameMangerS;
public Transform Target;
public float TransformSpeed;
public Animator CameraAnimator;
void Update()
{
NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y);
NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;
CurrentCoord.x = Mathf.SmoothDamp(CurrentCoord.x, NewCoord.x, ref vel.x, Smoothness / 2);
CurrentCoord.y = Mathf.SmoothDamp(CurrentCoord.y, NewCoord.y, ref vel.y, Smoothness / 2);
transform.rotation = Quaternion.Euler(CurrentCoord.x, CurrentCoord.y, 0);
}
And added this line to the controller script -
void FixedUpdate()
{
_body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
transform.rotation = Quaternion.Euler(0, MainCamera.CurrentCoord.y, 0);
}
When I'm standing the player normally rotates, but when I start to move, all rotations are reset and the player is not moving.
Update
Simple Rotation can be achieved using transform.Rotate().
Example:
this.transform.Rotate(Vector3.up, 30);
This example is gonna rotate the transform by 30° around the Vector that points upwards.
LookAtMouse:
To make your object turn towards the mouse, you need the ScreenToWorldSpace() method from your camera. In order to convert the ScreenCoordinates into your WorldCoordinates.
Example:
Please note:
You have to set the camera instance! If you don't add that, you'll get a NullReferenceException.
This Snippets shall only show the steps needed to achieve the behavior you wish.
You will have to find out yourself how to integrate that lines of code into your code to make it work. Consider what Programmer told you in the comment when integrating that.
Vector3 mousePosition = Input.mousePosition; //get the screenSpaceMousePosition
Vector3 worldPosition = this.camera.ScreenToWorldPoint(mousePosition); //convert it into worldSpacePosition
Vector3 calculatedDirection = worldPosition - transform.position; //calucate the looking direction
calculatedDirection.y = 0;
Quaternion rotation = Quaternion.LookRotation(calculatedDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);

Seperate Camera and Player Movement in Unity

I'm currently trying to script a camera that has the player in focus and can move around unlinked to the players own movement.
You can control the camera with the right xBox controller.
The camera should move normally on the X axis in an orbit around the player, but if you move the camera to look up or down (let's say 20° up) it should be on 0° to focus the player again if u let off the joystick and interpolate between those two angles.
In my script the camera is able to look up and down (but the degree isn't locked yet so you could move it on the Y axis 360°+) but it resets to 0°. Additionally the x axis doesn't behave as I want it to since it is able to rotate around the player but only 180° and then it stops and if u let the controller go it also sets it's values back (which I don't want for the x axis).
The camera is a child object of an empty game object. The script is attached to the empty game object.
I'm trying to figure this out since ~3 days and I'd be really grateful for any small advice.
Thanks in advance & here's my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowPlayer : MonoBehaviour
{
public GameObject target;
public GameObject cameraRig;
public float rotateSpeedX = 90.0f;
public float rotateSpeedY = 5f;
float cameraAngle;
float startCameraAngle;
Vector3 offset;
Vector3 currentOffset;
public float minRotationY;
public float maxRotationY;
bool isCameraMoving;
void Start()
{
offset = transform.position - target.transform.position;
cameraAngle = transform.localEulerAngles.z;
startCameraAngle = cameraAngle;
currentOffset = offset;
}
void Update()
{
transform.position = target.transform.position + currentOffset; // Folgt dem Spieler
float hor = Input.GetAxis("RightJoystickX") * rotateSpeedX; // Input rechter Joystick
float ver = Input.GetAxis("RightJoystickY") * rotateSpeedY;
if (!Mathf.Approximately (hor, 0)) {
transform.RotateAround (target.transform.position, Vector3.up, hor);
currentOffset = transform.position - target.transform.position;
}
if (!Mathf.Approximately (ver, 0)) {
cameraAngle += 110f * Time.deltaTime;
transform.RotateAround (target.transform.position, Vector3.up, hor);
currentOffset = transform.position - target.transform.position;
print(ver);
}
else if (ver <= 0.8f) {
//cameraAngle = Mathf.Lerp(cameraAngle, startCameraAngle, 1f - 0.1f * Time.deltaTime);
cameraAngle = startCameraAngle;
}
transform.localEulerAngles = new Vector3(0, hor, cameraAngle);
}
}
If sb else googled this and found my question this worked for me:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowPlayer : MonoBehaviour
{
public GameObject target;
public GameObject cameraRig;
public float rotateSpeedX = 9.0f;
public float rotateSpeedY = 5f;
float cameraAngle;
float startCameraAngle;
Vector3 offset;
Vector3 currentOffset;
bool isCameraMoving;
void Start()
{
offset = transform.position - target.transform.position;
cameraAngle = transform.localEulerAngles.z;
startCameraAngle = cameraAngle;
currentOffset = offset;
}
void Update()
{
transform.position = target.transform.position + currentOffset; // Folgt dem Spieler
float hor = Input.GetAxisRaw("RightJoystickX") * rotateSpeedX; // Input rechter Joystick GetAxis wenn Bewegung ausschwingen soll
float ver = Input.GetAxisRaw("RightJoystickY") * rotateSpeedY; // GetAxis wenn Bewegung ausschwingen soll
if (!Mathf.Approximately (hor, 0)) {
transform.RotateAround (target.transform.position, Vector3.up, hor);
currentOffset = transform.position - target.transform.position;
}
if (ver != 0) {
//cameraAngle += 110f * Time.deltaTime;
if (ver > 0)
{
cameraAngle += 110f * Time.deltaTime;
if (cameraAngle >= 420f)
{
cameraAngle = 420f;
}
}
if (ver < 0)
{
cameraAngle -= 110f * Time.deltaTime;
if (cameraAngle <= 290f)
{
cameraAngle = 290f;
}
}
transform.RotateAround (target.transform.position, Vector3.up, hor);
currentOffset = transform.position - target.transform.position;
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, cameraAngle);
}
else
{
cameraAngle = Mathf.Lerp(startCameraAngle, cameraAngle, 1f - 0.9f * Time.deltaTime);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, cameraAngle);
}

How can i rotate a spaceship back to it's original rotation to face the original position?

The spaceship start moving from point A. The spaceship is facing the moving direction.
Now when i click one on the L key i want that the spaceship will rotate and will face to the original position it was start moving from. But even if the spaceship is now rotated by axis Z or Y or X to rotate it first to the regular rotation values and to face to the start moving position.
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour
{
public int rotationSpeed = 75;
public int movementspeed = 10;
public int thrust = 10;
private bool isPKeyDown = false;
private float acceleration = .0f;
private Vector3 previousPosition = Vector3.zero;
private Rigidbody _rigidbody;
private Vector3 originalPosition;
private Quaternion originalRotation;
// Use this for initialization
void Start()
{
originalPosition = transform.position;
originalRotation = transform.rotation;
_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}
// Update is called once per frame
void Update()
{
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
transform.position += transform.forward * Time.deltaTime * movementspeed;
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (Input.GetKey("p"))
{
isPKeyDown = Input.GetKey("p");
float distance = Vector3.Distance(previousPosition, transform.position);
acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
previousPosition = transform.position;
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
}
if (Input.GetKey("l"))
{
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0);
//StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1));
//transform.position += transform.forward * Time.deltaTime * movementspeed;
}
}
IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
{
float lerpSpeed = 0;
while (lerpSpeed < 1)
{
ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
lerpSpeed += Time.deltaTime * smooth;
yield return null;
}
}
void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}
This is where i click the L button but i tried some things but can't yet find how to do it.
The main goal is if i click once on L the spaceship should automatic rotate if needed and move back to the original position and then land on ground. L stand for landing that's the main goal.
Add a variable on top -
...
private Vector3 originalPosition;
private Quaternion originalRotation;
private bool landShip = false;
...
And use following code in update function -
if (Input.GetKey("l"))
{
landShip = true;
//StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1));
//transform.position += transform.forward * Time.deltaTime * movementspeed;
}
if(landShip){
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0.5f);
}
Once the spaceship lands, set the landShip value back to false.

Rotate an object diagonally and prevent it from going back to 90,180,0,360 degrees

After holding down on a combination keys of AS,SD,DW or WA to move my object, it will successfully move diagonally and rotate to the correct position, but after releasing the keys, it will rotate back to either 0,90,180 or 360 depending on the nearest rotation after releasing my keys, I guess it's because of that 1 frame that I left the keys touched, so it ran that code and moved it to 0,90,180,360. But I don't know how to solve it.
I hope I've provided enough information, and thanks for helping out.
I'm pressing onto AW keys together
After releasing the keys, it doesn't stay that way but moves to either left or top
PlayerMovement.cs
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour {
private float speedWalk = 7.5f;
private float speedRotate = 7.5f;
private GameObject raycastObject;
// Update is called once per frame
void FixedUpdate ()
{
//Non-Diagonal Movements
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) //player movement up
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D)) //player movement down
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
//Diagonal Movements **########** I think this is the problem.
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W)) //player movement Top Left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) //player movement Top Right
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S)) //player movement Bottom Right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) //player movement Bottom Left
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
}
}
playerRotateMouse.cs
using UnityEngine;
using System.Collections;
public class playerRotateMouse : MonoBehaviour
{
public Transform Player;
private float speed = 7.5f;
Quaternion targetRotation;
void FixedUpdate()
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Generates a ray from cursor
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2"))
{
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Debug.DrawRay(transform.position, targetPoint, Color.green);
targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
}
}
// Smooth rotation towards point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
//Rotate base on key pressed
if (Input.GetKey(KeyCode.W) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate up
{
targetRotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate down
{
targetRotation = Quaternion.LookRotation(Vector3.forward * -1);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate right
{
targetRotation = Quaternion.LookRotation(Vector3.right);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate left
{
targetRotation = Quaternion.LookRotation(Vector3.left);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
I would save a last timestamp, and check if the second key release is within a certain timeframe. If it is release within (for example) 20ms, don't reset it is pressed.
For example: (pseudo)
private bool[] _directionKeysPressed = new bool[4];
private DateTime _previousKeyRelease;
private void KeyDown(object sender, EventArgs e)
{
_keyPressedCount++;
switch(keys)
{
case(W): _directionKeysPressed[0] = true;
case(D): _directionKeysPressed[1] = true;
// .................
}
}
private void KeyUp(object sender, EventArgs e)
{
_keyPressedCount--;
if(_keyPressedCount == 0)
{
// all keys released
_directionKeysPressed[] <--- last direction...
// reset all bools
return;
}
// ONLY if the key is released outside the 20 ms, reset the key.
if(_previousKeyRelease.AddMilliseconds(20) < DateTime.UTCNow)
{ // RESET KEY
switch(keys)
{
case(W): _directionKeysPressed[0] = false;
case(D): _directionKeysPressed[1] = false;
// .................
}
}
_previousKeyRelease = DateTime.UTCNow;
}
Personally I would do this with an bitfield in a Int32, but it is more clear with an array of bool.

Categories