How can I get my animation to play while moving forward? - c#

I have two animations, idle and jump. I want the jump animation to play at the same time as when the cube moves so it looks like it is moving up and forward but right now I can't get them to play together. Here is my code so far.
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour
{
private Vector3 newPos;
public Animator animator;
void Start(){
animator = GetComponent<Animator> ();
}
void Update(){
if (Input.GetKeyDown (KeyCode.UpArrow)) {
newPos = Vector3.forward + transform.position;
animator.SetBool ("jump", true);
transform.position = (newPos);
} else {
animator.SetBool ("jump", false);
}
}

The new position must be applied to the actual position :
transform.position = transform.position + (newPos);

Related

how can I prevent the camera from sliding after it rotates around the player

I am new to game developing, and I am building a game with an orthographic camera,
the camera suppose to be following the player and when I click and drag the mouse it supposes to turn around him.
I managed to make the camera turn around the player, but when I add the code for the mouse drag when I hold the mouse it turns but when I release it slides.
This is my C# code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFollow : MonoBehaviour {
public Transform PlayerTransform;
private Vector3 _cameraOffset;
[Range(0.01f, 1.0f)]
[SerializeField] float SmoothFactor = 0.5f;
[SerializeField] bool LookAtPlayer = false;
[SerializeField] bool RotateAroundPlayer = true;
[SerializeField] float RotationsSpeed = 5.0f;
// Use this for initialization
void Start () {
_cameraOffset = transform.position - PlayerTransform.position;
}
void LateUpdate () {
if (Input.GetMouseButtonDown(0))
{
RotateAroundPlayer = true;
}
else if (Input.GetMouseButtonUp(0))
{
RotateAroundPlayer = false;
}
if (RotateAroundPlayer)
{
Quaternion camTurnAngle =
Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
_cameraOffset = camTurnAngle * _cameraOffset;
}
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if (LookAtPlayer || RotateAroundPlayer)
transform.LookAt(PlayerTransform);
}
}

unity3d boxcollider2d not colliding with Vector2.MoveTowards

I am moving a sprite that has the following attached BoxCollider2D, Rigidbody2D (Dynamic set with simulated off) and a SpriteRenderer. Problem the trigger is not being set with the following code. I come from a 3D background and this would have triggered but its not in 2D not sure why, maybe things are different?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterController : MonoBehaviour
{
private MonsterInformation info;
private bool isMoving;
private Vector3 wantedPosition;
private Vector3 MaxPosition;
private Vector3 MinPosition;
private bool isAttacking;
// Start is called before the first frame update
void Start()
{
info = MonsterDatabase.Instance.MonsterLookup("Slime");
MaxPosition = new Vector3(transform.position.x + info.WanderingDistance,
transform.position.y + info.WanderingDistance, transform.position.z);
MinPosition = new Vector3(transform.position.x - info.WanderingDistance,
transform.position.y - info.WanderingDistance, transform.position.z);
}
public void Update()
{
if(!isMoving)
{
wantedPosition.x = Random.Range(MinPosition.x, MaxPosition.x);
wantedPosition.y = Random.Range(MinPosition.y, MaxPosition.y);
wantedPosition.z = transform.position.z;
isMoving = true;
}
if(isAttacking)
{
return;
}
transform.position = Vector2.MoveTowards(transform.position, wantedPosition,
Time.deltaTime * info.WalkingSpeed);
if(transform.position == wantedPosition)
{
isMoving = false;
}
}
public void OnTriggerEnter2D(Collider2D other)
{
Debug.Log($"Triggered {other.name}");
}
}
I am guessing that this needs to be moved through physics? transform.position = Vector2.MoveTowards(transform.position, wantedPosition,
Time.deltaTime * info.WalkingSpeed);
Also thank you for your time to respond to this question even if it seems like its stupid and I didn't do enough research, the main problem is Unity3D in a 3D environment I would collide with a box collider without having to move through physics so I am stumped as to why its not working in 2D.

With what I should replace dirX for my bullets to shoot right and left

I am doing a 2d game and I want to know how I can make the bullets to shoot right and left . At this moment the bullets go just to the left , even if my player moves right. How I can make them shoot both sides or shoot just when they find an object tagged " enemy "
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Character : MonoBehaviour
{
Rigidbody2D rb;
float dirX;
[SerializeField]
float moveSpeed = 5f, jumpForce = 400f, bulletSpeed = 500f;
Vector3 localScale;
public Transform barrel;
public Rigidbody2D bullet;
// Use this for initialization
void Start()
{
localScale = transform.localScale;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}
void FixedUpdate()
{
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
}
void Jump()
{
if (rb.velocity.y == 0)
rb.AddForce(Vector2.up * jumpForce);
}
void Fire()
{
var firedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
firedBullet.AddForce(barrel.up * bulletSpeed);
}
}
You're already accounting for the direction of the barrel. You just need to change the direction of the barrel when you move.
One way to do that is to just set that directly:
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (dirX !=0)
{
barrel.up = Vector3.right * Mathf.Sign(dirX);
}
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}
If the barrel is a child of the player object, then changing the character's rotation so that the barrel's direction points in the correct direction will also work. There's not enough information in the question to know for sure but maybe using Quaternion.LookRotation to set the character's rotation like so would work:
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (dirX !=0)
{
Vector3 newPlayerForward = Vector3.forward * Mathf.Sign(dirX);
transform.rotation = Quaternion.LookRotation(newPlayerForward, Vector3.up);
}
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}

Animation Starts but player won't move on key press in Unity

Me and 3 friends started working on game and while codding run into error that animation on player starts but it won't move. We are working in Unity(C#)
I already checked for names in Animator and code so it's not it.
Here is a code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed = 5f;
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f , 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
if(Input.GetAxis("Horizontal") > 0){
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
}
void Jump(){
if (Input.GetButtonDown("Jump")){
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f,5f), ForceMode2D.Impulse);
}
}
}
It's 2D game if It matters
You could try to create the rigidbody2d in start(), then try
rigidbody.AddForce(Vector2(moveSpeed,0), ForceMode2D.Impulse);

How can I make the npc character to rotate slowly facing the player while changing animator transitions?

I have in the soldier animator a Grounded transition and this is start with this transition by default. Then I did that after 3 second it will slowly change between the two transitions from Grounded to Rifle_Aiming_Idle.
But To see how it's working and if it's working good like I wanted I had to turn off the FPSController Camera( FPSCamera is turned off gray ). So when running the game the active camera is the Main Camera and the CM vcam1 ( Cinemachine virtual camera ).
But I want to make more with this cutscene.
I want that first the cutscene will start only when the player the FPSController will exit a door:
This script is attached to the FPSController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform npc;
public float cutsceneDistance = 5f;
public float speed;
private bool moveNpc = false;
// Use this for initialization
void Start()
{
}
private void Update()
{
if (moveNpc)
{
float travel = Mathf.Abs(speed) * Time.deltaTime;
Vector3 direction = (player.position - npc.position).normalized;
Quaternion lookrotation = Quaternion.LookRotation(direction);
npc.rotation = Quaternion.Slerp(npc.rotation, lookrotation, Time.deltaTime * 5);
Vector3 position = npc.position;
position = Vector3.MoveTowards(position, player.position, travel);
npc.position = position;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.name == "Horizontal_Doors_Kit")
moveNpc = true;
}
}
I want that when the player the FPSController exit the door start the cutscene and while it's playing/doing the cutscene I want at the same time also making the soldier with the animator to rotate slowly facing the FPSController.
FPSController exit the door > Soldier changing slowly from grounded to aiming mode and Soldier rotating slowly facing the FPSController.
My problems are how to start the cinemachine cutscene when the FPSController exit the door and how to make the Soldier( npc ) to rotate slowly facing the FPSController.
What I tried in the Update in the script is not working good.
It's making the npc moving and not rotating slowly good enough fro the cutscene like I wanted.
And maybe for the rotation part I should use the Cinemachine and not doing it in the script ? Maybe I should using Timeline here too for the rotation part ?
Working solution for what I needed:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform npc;
public Camera FPSCamera;
public Camera mainCamera;
public Animator anim;
public float rotationSpeed = 3f;
private bool moveNpc = false;
// Use this for initialization
void Start()
{
}
private void Update()
{
if (moveNpc)
{
Vector3 dir = player.position - npc.position;
dir.y = 0; // keep the direction strictly horizontal
Quaternion rot = Quaternion.LookRotation(dir);
// slerp to the desired rotation over time
npc.rotation = Quaternion.Slerp(npc.rotation, rot, rotationSpeed * Time.deltaTime);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.name == "Horizontal_Doors_Kit")
{
FPSCamera.enabled = false;
mainCamera.enabled = true;
moveNpc = true;
anim.SetTrigger("SoldierAimingTrigger");
}
}
}

Categories