I look for the best way to detect, what site of an GO (cube) is facing upwards.
My research led me to the Dot-product.
I know what I want to do, but I guess my c# skills are too bad..
Basically I just want to find the Dot-product for example for the x-rotation.
And if its between a value of 0,9 to 1 one site is facing upwards, for -0,9 to -1 the other Site and so on for all axes.
In the Unity's documentation you have the following example. You just have to tweak it a little bit in order to achieve what you need.
// detects if other transform is behind this object
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Update()
{
if (other)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = other.position - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
{
print("The other transform is behind me!");
}
}
}
}
Related
Im working on a game similar to archvale and enter the dungeon and I'm trying to create a dash function. Ive tried using force and some other methods I've searched for online but I haven't been able to find anything. Any ideas?
This game is a similar perspective to ETG being a 3D game using 2D sprites in a top down format.
Thanks!
EDIT: This is code I tried using but it didn't work at all. It is code I found online.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dash : MonoBehaviour
{
public class PlayerMovement : MonoBehaviour
{
public float dash = 3000f;
public bool DashTime = true;
// Use this for initialization
void Start()
{
bool DashTime = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("f"))
{
GetComponent<Rigidbody>().AddForce(new Vector2(dash, 0), ForceMode.Force);
StartCoroutine(LateCall());
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(0.05f);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX;
yield return new WaitForSeconds(0.06f);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
}
}
}
Without any code or anything to look at its kind of hard to answer the question. You could do a transform.translate or you could teleport based on where the player is looking or the mouse location. There are a ton of ways you could create a dash effect but without anything to go on it's really up in the air as to what solution you come up with.
You can check out this tutorial and it might help you:
https://generalistprogrammer.com/unity/unity-2d-dash-movement-effect-learn-to-how-to-tutorial/
I am very new at C# (around 1 week in), so I have been following a lot of tutorials. I am at the moment trying to edit a piece of code (unity tutorial) to only have the enemy follow the player when in a certain range of the player (10 foot), but unfortunately without changing the code completely, I cannot find a solution. At the moment the enemy will only follow the player when the player is alive (which i also want).
I've tried googling what I want, but I don't want to change the code from what is it too much. I am very new at c# and and currently learning bit by bit, other methods I have found require me changing the code a lot. As far as I understand, the main focus of the code below is for the enemy to be completely controlled by the Nav Mesh Agent, is it possible for me to keep the same technique?
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
void Awake()
{
// references
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination(player.position);
transform.LookAt(player);
}
else
{
nav.enabled = false;
}
}
}
If possible, I would like to add 2 if functions. 1 being the check if the player is alive, and the other to check if the player is within distance. I don't know much about raycast at the moment, so will this be the next step for me to learn to get this to work how i want?
Thankyou for your time.
Dean.
You can calculate distance by doing:
float distance = Vector3.Distance(player.transform.position,transform.position);
You can do a check if it's no larger than some amount with:
bool playerIsCloseEnough = distance <= amount;
And you can check if the player is alive with:
bool playerIsAlive = playerHealth.currentHealth > 0;
Then, you can do things if they're both true with:
if (playerIsAlive && playerIsCloseEnough)
{
// ...
}
And as you mentioned in the comment, you'll need to set nav.enabled=true; in void Awake or void Start to make sure the nav mesh is turned on :)
I'm making a 2D platformer, and I want the camera to follow the character once it gets to the center, but follow it only on the x axis. I've downloaded this code, and it worked, but it followed the character on both the x and the y, and it stuck the character in the same corner of the camera that it started. I tried to add an if statement so that the camera only started moving once the offset was equal to the player, but it didn't work. I got the error code 'cannot implicitly convert vector3 to bool'
Here is the code:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
if (offset == player.transform.position)
{
transform.position = player.transform.position + offset;
}
}
}
The documentation seems to suggest that what you are doing is fine. But perhaps your code could be modified to fit this example? My gut says that this is going to give you the same error, but it could be worth trying.
https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Example()
{
if (other && transform.position == other.position)
{
print("I'm at the same place as the other transform!");
}
}
}
You could also try using the Vector3.Equals() method instead if you're ok with doing an exact comparison.
https://docs.unity3d.com/ScriptReference/Vector3.Equals.html
You could also try implementing your own function to compare the two vectors.
https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html
I really can't see anything wrong with the code you've posted, is it possible the error exists in another file?
So I wrote some code to make an object rotate if I swiped left or right
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotater : MonoBehaviour {
public Transform player;
void Update()
{
if (Input.touchCount == 1)
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
player.transform.Rotate(0f, 0f, touch0.deltaPosition.x);
}
}
}
}
and the problem is when I swipe fast the rotation will be uncontrollable. So I want the input to be less sensitive.
my goal is for the rotation to be like rolly vortex
my setup:
I made an empty object and put it in the center
made the empty object a parent of my player
and finally, I put my code in the empty object
this setup made the player rotate in sort of an orbit which like I told you is similar to rolly vortex.
First you want to be able to scale the sensitivity. This means making it so that for every unit of change in touch position, you will get some multiple of a unit of change in the rotation. For this, make a configurable (public) member variable, public float touchSensitivityScale, and multiply the rotation by that value. Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotater : MonoBehaviour {
public Transform player;
public float touchSensitivityScale;
void Update()
{
if (Input.touchCount == 1)
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
player.transform.Rotate(0f, 0f, touch0.deltaPosition.x * touchSensitivityScale);
}
}
}
}
Now you can edit the touch sensitivity in the inspector. With touchSensitivityScale set to 1, the behavior will be identical to what it is currently. If you make the number 0.5, the rotation will be half as sensitive.
If this doesn't fully solve the problem and you also want some smoothing or acceleration, that might warrant an edit to the question.
I hope it helps!
Rather than rotating by touch0.deltaPosition.x you could always have some sort of negative exponential function. In this case it’d probably be something along the lines of e^(-x-a) where x is your touch0.deltaPosition.x, and a would be a variable you’d have to determine based on how fast you want the initial speed of rotation. If you’re not familiar with exponential functions try using a graphing software like Desmos to plot y=e^(-x-a) and vary the value of a. Once you’ve visualised that it should be pretty self explanatory.
I can already make the movement grid via pathfinding and avoid obstacles for the player. Now I want to make the AI move itself based on the movement grid and available action points (just like the player does), but I don't know how to do it. Right now, I am only able to make the character move to the position (but it is not follow the pathfinding, this character suppose to be the AI). I am stuck trying to solve this problem, but couldn't. Could you guys help me out? Thanks.
Here is the code I've got so far (it makes the character which is suppose to be AI to move to the position, but it not follow the pathfinding):
using UnityEngine;
using System.Collections;
public class AIPlayer : Player
{
void Awake()
{
moveDestination = transform.position;
}
// Use this for initialization
void Start()
{
ColorChanger();
}
// Update is called once per frame
void Update()
{
}
public override void TurnUpdate()
{
if (GameManager.instance.currentPlayerIndex == 5)
{
if (Vector3.Distance(moveDestination, transform.position) > 0.1f)
{
transform.position += (moveDestination - transform.position).normalized * moveSpeed * Time.deltaTime;
if (Vector3.Distance(moveDestination, transform.position) <= 0.1f)
{
transform.position = moveDestination;
actionPoints--;
}
}
else
{
moveDestination = new Vector3(2 - Mathf.Floor(GameManager.instance.mapSize / 2), 1.5f, -2 + Mathf.Floor(GameManager.instance.mapSize / 2));
GameManager.instance.NextTurn();
}
}
base.TurnUpdate();
}
public override void TurnOnGUI()
{
}
public override void ColorChanger()
{
base.ColorChanger();
}
}
Here is the link video for the game:
http://www.youtube.com/watch?v=eo7DzbBPQBs&feature=youtu.be
There's no code for pathfinding there. It's just an overly complicated lerp. As well, moving directly to the position will cause major headaches with graphical obstacle avoidance.
Your best bet is to implement a grid-based navmesh and use something like an A* search to find the path. The movement would be limited to adjacent squares, so graphical obstacle avoidance wouldn't happen.
Have a look at this tutorial. It'll give you everything you need except for animating player movement and the game logic for the target position.