Make object follow rotation with very little difference - c#

I am making my object follow the rotation of another object. I want my object to rotate with very little difference, that is, from its current rotation it should not rotate completely with the other object.
There should be a difference u pto its rotation such that it should rotate 10% of what the main object rotates. How do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowRotationWithLimit : MonoBehaviour {
public GameObject objectToFollow;
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.eulerAngles = new Vector3 (this.transform.eulerAngles.x, objectToFollow.transform.eulerAngles.y, this.transform.eulerAngles.z);
}
}
I am not able to get that logic of creating that difference.

I would rather call it FollowRotationWithFactor then and simply use a multiplier
public class FollowRotationWithFactor : MonoBehaviour
{
public GameObject objectToFollow;
public float factor = 0.1f;
// Update is called once per frame
void Update ()
{
var eulerAngles = transform.eulerAngels;
eulerAngles.y = objectToFollow.transform.eulerAngles.y * factor;
transform.eulerAngles = eulerAngles;
}
}

Related

I'm trying to add multiple camera switches to a vehicle in Unity and I am not sure how to code it

My project is from the "Create with Unity" tutorial unit 1 with a simple vehicle moving down a road. I originally had 1 camera which worked, but when I changed the code to work with multiple cameras I couldn't get the compiler to work.
The 3 cameras are supposed to display different angles of the tank while controlling the vehicle. I assume something is wrong with how I wrote the FollowPlayer.cs "void LateUpdate()" method, but I don't know how to make it work. It all went wrong when I changedthe FollowPlayer class from 1 cam to 3 cams.
The picture shows the unity visuals and all relevant class coding is below.
I'm new to unity and c#, so any help would be appreciated!
MY CAM SWITCH CLASS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamSwitch : MonoBehaviour
{
public GameObject cam1;
public GameObject cam2;
public GameObject cam3;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Switch1"))
{
cam1.SetActive(true);
cam2.SetActive(false);
cam3.SetActive(false);
}
if (Input.GetButtonDown("Switch2"))
{
cam1.SetActive(false);
cam2.SetActive(true);
cam3.SetActive(false);
}
if (Input.GetButtonDown("Switch3"))
{
cam1.SetActive(false);
cam2.SetActive(false);
cam3.SetActive(true);
}
}
}
THE CLASS I THINK I MESSED UP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
public Vector3 offset1= new Vector3(0, 5, -7);
public Vector3 offset2 = new Vector3(0, 3.83, -7);
public Vector3 offset3 = new Vector3(0, 7.68, -11.8);
public GameObject cam1;
public GameObject cam2;
public GameObject cam3;
cam1.SetActive(true)
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
if (cam1.SetActive(true))
{
transform.position = player.transform.position + offset1;
}
if (cam2.SetActive(true))
{
transform.position = player.transform.position + offset2;
}
if (cam3.SetActive(true))
{
transform.position = player.transform.position + offset3;
}
}
}
First: remove the line cam1.SetActive(true) under public GameObject cam3;. Its missing a semicolon and it must be placed in a function scope anyway, not in class declaration scope.
Second: this statement (and the other two) if (cam1.SetActive(true)) cant work because SetActive does not return a bool value. You probably wanted if (cam1.activeSelf).
You may spend some time improving your knowledge C# coding basics in Unity

Why is Instantiate only cloning four times?

My issue here is that my object (a 500 long cube with a trigger on top) is only cloning four times with Instantiate(). Can anyone explain as to why? Is there a limit to how far you can clone items?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class groundGen : MonoBehaviour
{
public Transform player;
public Object ground;
public Transform groundLoc;
public int distance = 500;
public void OnTriggerEnter(Collider checkPass)
{
if (checkPass.tag == "checkpoint")
{
Instantiate(ground, groundLoc.position + new Vector3(0, 0, distance), groundLoc.rotation);
distance += 500;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
There is no real limit.
But:
there is some inaccuracy due to floating point numbers, if your coordinates get really big.
Your meshes could get clipped by Camera. Log your Instantiate Calls and check hierarchy in Playmode. Increase Cameras far-plane if needed.

Unity (C#) - How do I single out GameObject being detected by Raycast in a destroy / respawn system

I'm trying to make a Destroy gameobject, wait x seconds, respawn gameobject system. I have 2 scripts and I'm destorying then instantiating it again. I want to use multiples of the same prefab called "Breakable" but have only the one I'm aiming at being destroyed. Similar to games like Minecraft, aim and only the aimed at the block is destroyed.
BlockBreakItem script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockBreakItem : MonoBehaviour
{
RaycastHit hit;
int layerMask = 1;
public GameObject breakableObject;
public bool isObjectDestoryed = false;
public int score = 0;
// Update is called once per frame
void Update()
{
breakableDetection();
}
void breakableDetection()
{
Ray rayLocation = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayLocation, out hit, 1000, layerMask))
{
print("Detected");
if (Input.GetKey(KeyCode.Mouse0))
{
breakableObject = GameObject.Find("Breakable");
Destroy(breakableObject);
isObjectDestoryed = true;
score = score +1 ;
}
}
}
}
RespawnBrokenObject script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RespawnBrokenObject : MonoBehaviour
{
private BlockBreakItem BlockBreakItem;
public GameObject breakablePrefab;
// Start is called before the first frame update
void Start()
{
BlockBreakItem = GameObject.FindObjectOfType<BlockBreakItem>();
}
// Update is called once per frame
void Update()
{
if (BlockBreakItem.isObjectDestoryed == true)
{
Invoke("respawnObject", 5.0f);
BlockBreakItem.isObjectDestoryed = false;
}
}
void respawnObject()
{
GameObject tempObjName = Instantiate(breakablePrefab);
tempObjName.name = breakablePrefab.name;
BlockBreakItem.breakableObject = tempObjName;
}
}
I hope the code isn't too messy! Always worried it won't be understood xD
Fortunately it's easy and basic in Unity!
You don't do this:
breakableObject = GameObject.Find("Breakable");
The good news is the cast will tell you what object you hit! Great, eh?
It's basically:
hit.collider.gameObject
You can use hit.collider.gameObject.name in a Debug.Log for convenience.
It's that easy!
Note you then (if you need it) get your component on that object with something like .GetComponent<YourBreakishBlock>()... easy.
Note that you can CHECK if the object hit, is one of the "YourBreakishBlock" objects, by simply checking if that component is present.
Note simply google something like "unity physics raycast out hit, which object was hit ?" for endless examples,
https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/changing-properties-of-object-hit-with-raycast.538819/
etc.
--
Make this simple class:
public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}
Now put that on SOME cubes but NOT on OTHER cubes.
In your ray code:
ExamplePutThisOnACube teste =
hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();
and then check this out:
if (teste != null) { teste.SAYHELLO(); }
Now run the app and try pointing at the various cubes!

Unity C# problem with camera, camera goes white

I made a script for my camera for following the player.When I play the game, game view goes white, even if on scene view all is fine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos;
}
}
The problem might be that the player is blocking the camera (because the camera is inside the player). Try adding some offset by adding a Vector3 as a variable and adding it on to the transform.position.
The offset could be used so that the camera is in front of the player, or in a third person angle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos + offset;
}
}
Hope this helps.

Give every prefab his own speed

Is was building a star background simulation for a spaceshooter game in UNITY3D.
In c# i built The Instantitiation of prefabs which look like dots. While the generation of multiple dots on random spots on the x-axis go perfectly i wanted to add one thing.
Random speed. The problem is that i don't know how to give every prefab his own speed en keep it instead of getting overwritten by the next randomnization.
backgroundloop.cs
using UnityEngine;
using System.Collections;
public class backgroundloop : MonoBehaviour {
public GameObject star;
public float spawnTime;
private GameObject starPrefab;
private float timestamp;
//public float hoogte = Random.Range(-1.01f,1.1f);
//public float rotationPrefab = transform.localEulerAngles.z;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Time.time > timestamp) {
starPrefab = Instantiate
(star, new Vector3(7,Random.Range(-5.0f,5.0f),0),
Quaternion.Euler(0, 0, 0)) as GameObject;
timestamp = Time.time + spawnTime;
}
}
}
And the problem is in this script:
prefabMovement.cs
using UnityEngine;
using System.Collections;
public class prefabMovement : MonoBehaviour {
float speed = Random.Range (-3f, -0.1f);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(-1f,0,0) * Time.deltaTime);
if (transform.position.x < -6.8) {
Destroy (this.gameObject);
}
}
}
Huh? So the speed is overwritten and eventually ends up being the same on all instantiated prefabs?
Maybe it helps if initialize the speed variable inside the Start method.

Categories