I have an enemy on Unity, the enemy follows the player and when player is at a specific distance i want the enemy run X units without stopping. The idea is that the enemy run, then he gets tired and the Player can attack him from behind.
How can i make enemy run X distance forward without stopping?
How can i make enemy run X distance forward without stopping?
Depends on a lot of things, how is the movement handled in your game? is it 2D? 3D? etc. ...
If you use a character controller on your Enemy GameObject for example (in 2D)
Vector2 moveDirection = Vector2.zero;
void Update(){
CharacterController player = GetComponent<CharacterController>();
if (player.isGrounded)
{
// constantly move horizontally
moveDirection.x = valuespeed;
player.Move(moveDirection * Time.deltaTime);
}
}
This is just some basic theory. FOr 3D, you add a dimension and choose the direction by using the current forward vector or so.
As for checking the X value, what you could do is that in your Enemy class, when the "running mode" is triggered, you save the origin position and in the update, you check the distance between CurrentPosition and Origin (Vector3.Distance(......)) and as long as the distance is smaller than X, you let him run. If not, you are not in "running mode".
Related
I am a newbie to unity. In my project, the ball should instantiate in the player
position and it should go randomly 40 to 120 degrees from the player position.
I am using transition.forward. My player is constantly in running. when the player stands the ball is going correctly in front of the player.
but when the player runs the ball is going backward
How can I move towards randomly in front of the player? Like a ball
I am using the below code.
transform.Translate(Vector3.forward*5f );
There is a chance it might be fixed if you spawn the ball in front of the character. Since it works when the player is stationary, it might be conflicting with the collider of player when it is moving.
Also translating will just move the ball, not give it a velocity or anything. You should take a look at https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
So, first translate the ball forward and see if it spawned in the right location.
Then add a force vector to the ball's rigid body in the direction you like.
Something like:
ball.transform.Translate(Vector3.forward*5f);
float thrust = 10;
var rb = ball.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * thrust);
How can I make a camera in Unity, that repeats the rotation and position of another camera on all three axes?
I'm thinking about portals - how to repeat players camera movement and rotation by another camera which renders texture for the portal, to create a realistic effect while player moves - that there is a whole new scene behind the portal.
Imagine I have a player camera and another camera in another place in the scene. The second camera may have different position and rotation initially. But when the player camera rotates 90 degrees to the left, the second camera should add +90 degrees to the left to its current rotation.
And the same with movement, so if the player moves 1 meter forwards, the camera moves 1 meter forwards from its current position.
You can create a script that takes a transform as external input which copies the values from one object to the other. If you want to keep the offset, so just look and move in the same direction, but not be at the same location that is also possible.
The following script lets you mimic another object:
public class Mimic : MonoBehaviour
{
[SerializeField]
private Transform other;
private Vector3 offset;
private void Start()
{
offset = transform.position - other.position;
}
private void Update()
{
transform.rotation = other.rotation;
transform.position = other.position + offset;
}
}
I am working on a game which has strong base on spherical gravity. But somehow my code is not working as expected. So please have a look at my code and tell me how can I make my spherical gravity work.
public class CircularGravity : MonoBehaviour {
private Rigidbody2D rigid;
[SerializeField]
Transform planet;
[SerializeField]
float acceleration = 0.81f;
// Use this for initialization
void Start () {
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
rigid.AddForce((planet.position - transform.position).normalized * acceleration);
transform.rotation = Quaternion.LookRotation(planet.position - transform.position, transform.up);
}
}
Problem elaboration. Project is 2d. it contains a circle sprite as a planet with a collider and hexagon has a player(just a prototype) with collider and rigidbody. This script is attached to the player(hexagon). According to my logic rigidbody should apply a force to the player and push it towards the planet and it should face the planet. So even if the player is on the downward side of circle it shouldnot fall instead it shall be pushed toward the planet. But all the colliders are being neglected and player is just going to strange position
First things I would check...
Make sure the origin of your sprite is actually in the center of the image. If it's in the corner the player will be pulled toward the corner.
Make sure both the planet and player have rigid bodies and colliders. Click on the collider and make sure the edge lines up with you shapes.
Make sure both the planet and the player have their z set to zero. Lock z in both rigidbodies.
I'm using my camera as a child gameobject of my ball, so when I move my ball camera comes with him. But the problem is I'm using rigidbody.addForce(), so when ball rotates the camera rotates with it, too.
So what should I do not to rotate my camera but only move it with my ball?
void FixedUpdate()
{
rigidbody.addForce(Input.getAxis("Horizontal"), 0, Input.getAxis("vertical"));
}
There are several ways to solve this, I'll list a few.
The first, if you don't care if your ball rotates or not, you can just disable the rotation on the ball. If you open the Constraints section in the rigidbody component, you can freeze the rotation so that the ball won't rotate.
Alternatively, you can write a script that keeps the camera always oriented a certain way. Depending on if you want the camera to rotate around the ball on any plane depends on the way you would implement this.
The third option, which is cleanest, is to not have the camera be a child of the ball. A minimal component to do this would look like this:
public class TargetFollow : MonoBehaviour
{
public Transform Target;
public float DistanceFromTarget;
void Update()
{
transform.position = Target.position + new Vector3(0, 0, DistanceFromTarget);
transform.LookAt(Target);
}
}
Just drag your ball into the 'Target' slot in your component. Keep in mind, this is super bare bones. You may want to add more variables to better control the direction the camera should be from the ball, or perhaps something that grabs a snapshot of the direction and distance the camera is from the ball in the Start method.
I have several formulas I use for firing a bullet at a target, but it all breaks down when the player moves from his original position and the bullet reaches the old position of the player as intended.
When PlayerPosition == BulletPosition, how do I make the bullet keep going in the right direction if it misses? My problem is once the bullet reaches where it was supposed to go it stops and I need a new formula to keep it moving.
If it hits the player, that's easy, remove the item, but I can't seem to find a good solution. Below is some code, it's super simple for now.
var movement = PlayerPosition - Position;
if (movement != Vector2.Zero)
movement.Normalize();
//var angle = Math.Atan2(movement.Y, movement.X);
Position += movement*_projectileMoveSpeed;
Did you intend the bullet's speed to be affected by the distance from the player?
I'd just save the velocity, then use that in the future frames. In pseudocode:
to shoot a bullet:
direction is sign(PlayerPosition - Position)
in each frame:
for each bullet:
modify position by direction * projectileMoveSpeed
handle collision (player or screen edge)