using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform target;
private void Update()
{
transform.LookAt(target);
}
}
I need to delete Y rotation in LookAt, help me!
(Im add some iformation cose STACKL OVERFLOW want some more information from me)
im guessing what you're implying is zeroing out the y axis.
in that case, after you call your LookAt, you want to reset the y axis
YOURS
private void Update()
{
transform.LookAt(target);
//reset the y axis here.
}
FIXED
private void Update()
{
transform.LookAt(target);
transform.rotation.y = 0;
//make sure to call that after LookAt, otherwise it will override the other line
}
i'd recommend you watch a few tutorials instead of being fed code.
Related
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;
}
}
I'm trying to make a 2D sprite flip in a 3D scene, whenever the camera reaches an Y rotation of 180.
I can't get it to work, even though the TEST debug text shows whenever it reaches 180.
This is the code I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate_enemy : MonoBehaviour {
[SerializeField] SpriteRenderer spriteRenderer;
public bool flipX;
void Update()
{
float fYRot = Camera.main.transform.eulerAngles.y;
if (fYRot >= 180)
{
Debug.Log("TESTING");
spriteRenderer.flipX = true;
}
else
{
spriteRenderer.flipX = false;
}
}
}
I have a similiar script, that instead changes rotation based on velocity (the direction to which the character is going), and that script works just fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sprite_flip : MonoBehaviour {
[SerializeField] SpriteRenderer spriteRenderer;
public bool flipX;
Vector3 pos, velocity;
void Awake()
{
pos = transform.position;
}
// Update is called once per frame
void Update()
{
velocity = (transform.position - pos) / Time.deltaTime;
pos = transform.position;
if (velocity.x >= 0)
{
spriteRenderer.flipX = true;
}
else
{
spriteRenderer.flipX = false;
}
}
}
Anyone who might have an idea why the Rotate_Enemy script isn't doing what I want to achieve? Tried to find solutions but I couldn't make any of them work, so there must be something I don't understand. Super grateful for any help! :)
I've been through number of answers on this topic, but nothing has seemed to work for my case. I'm trying to detect a mouseDown on a UI element with a canvas renderer which is inside the hierarchy of an object with the canvas on it. I'm new to this, so I'm not sure if the canvas needs to be linked to this canvas renderer or if they have to be on the same object, but the following code is not resulting in the OnPointerDown method being activated.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class Knob : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public Slider slider;
public GameObject pivot;
public bool selected;
public float z;
public Image image;
public UtilityClass the;
public float min;
public float max;
void Start () {
z = 90;
Quaternion rotation = Quaternion.Euler (0, 0, z);
pivot.transform.rotation = rotation;
}
void Update() {
Vector3 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (selected) {
float pivotAngle = the.AngleFrom (the.Heading (pivot.transform.position, mousePosition));
if (pivotAngle >= min && pivotAngle <= max)
z = pivotAngle;
Quaternion rotation = Quaternion.Euler (0, 0, z);
pivot.transform.rotation = rotation;
}
}
public void OnPointerDown(PointerEventData eventData) {
selected = true;
}
public void OnPointerUp(PointerEventData eventData) {
selected = false;
}
}
At the moment I don't have a Collider 2D on the object, but I have "Raycast Target" selected in the Image script. Any help would be appreciated.
Credit to Juan Bayona Beriso I was missing an Event System Component.
So this is the code which I use to generate the pipe thingies, but after it generates it I want to destroy it after its out of screen or any other possible way, maybe after 3 seconds or any way possible and I want this pipe thingies to generate randomly in different sizes you get me ryt just like flappy bird.
using UnityEngine;
using System.Collections;
public class Generate : MonoBehaviour {
public GameObject lightnest;
public float initialDelay;
public float finalDelay;
public float rampDuration;
protected float _delay;
protected float _runTime;
protected float _timeSinceSpawn;
// Use this for initialization
void Start () {
_delay = initialDelay;
_runTime = _timeSinceSpawn = 0.0f;
}
// Update is called once per frame
void Update() {
_runTime += Time.deltaTime;
_timeSinceSpawn += Time.deltaTime;
_delay = Mathf.Lerp(initialDelay, finalDelay, _timeSinceSpawn / rampDuration);
if (_timeSinceSpawn > _delay) Spawn();
}
protected void Spawn()
{
_timeSinceSpawn = 0.0f;
Instantiate(lightnest);
}
}
Use gameobject.transform.localScale to change the size on Start. And use gameobject.transform.position to check if the object is out of the screen. And use Destroy(gameobject) to destroy the object.
https://docs.unity3d.com/ScriptReference/Transform.html
Hope this helps!
I recommend you to watch this tutorial.
Object pooling is the most efficient way handle this kind of situtation.
I'm trying to make enemies follow my player when the player enters the radius area of an enemy, but make the enemy stop following when my bullet hits object or enters radiusArea.
See my gif for more detail:
Gif
script:
using UnityEngine;
using System.Collections;
public class FlyEnemyMove : MonoBehaviour
{
public float moveSpeed;
public float playerRange;
public LayerMask playerLayer;
public bool playerInRange;
PlayerController thePlayer;
// Use this for initialization
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update()
{
flip();
playerInRange = Physics2D.OverlapCircle(transform.position, playerRange, playerLayer);
if (playerInRange)
{
transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, moveSpeed * Time.deltaTime);
//Debug.Log(transform.position.y);
}
//Debug.Log(playerInRange);
}
void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, playerRange);
}
void flip()
{
if (thePlayer.transform.position.x < transform.position.x)
{
transform.localScale = new Vector3(0.2377247f, 0.2377247f, 0.2377247f);
}
else
{
transform.localScale = new Vector3(-0.2377247f, 0.2377247f, 0.2377247f);
}
}
}
I hope someone can help me :(
Physics2D.OverlapCircle detects only the collider with the lowest z value (if multiple are in range). So you either need to change the z values so the player has the lowest or you need to work with Physics2D.OverlapCircleAll and check the list to find the player. Or you could change your layers so only the player itself is on that specific layer you feed into the overlap test.