how do i drag and launch in untiy 2d? - c#

I am a newbie in c# and i have been trying to get some code that will help me launch my character in the opposite direction of the drag. Like Angry Birds but i don't want the character to move while i am dragging. Here is my script that i copied from a video and it isn't working.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbMovement : MonoBehaviour
{
public float launchSpeed = 100;
Vector3 _initialPosition;
Rigidbody2D rb;
private Vector2 directionToInitialPosition;
private void Awake()
{
_initialPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
directionToInitialPosition = _initialPosition - transform.position;
}
private void OnMouseUp()
{
rb.AddForce(directionToInitialPosition *launchSpeed);
}
private void OnMouseDrag()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(newPosition.x, newPosition.y);
}
}
`
help me with a script that will launch my character opposite of the drag. Thanks in advance :)

Why don't you just remove the
private void OnMouseDrag()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(newPosition.x, newPosition.y);
}
this part of the script means that everytime the player drag the mouse/touch then the gameobject will follow the mouse. Because you don't want it, you can just remove it to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbMovement : MonoBehaviour
{
public float launchSpeed = 100;
Vector3 _initialPosition;
Rigidbody2D rb;
private Vector2 directionToInitialPosition;
private void Awake()
{
_initialPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
directionToInitialPosition = _initialPosition - transform.position;
}
private void OnMouseUp()
{
rb.AddForce(directionToInitialPosition *launchSpeed);
}
}

Related

Change AI Enemy Animation

Currently, I'm making a game in unity, and I made a simple script to make the enemy follow the player but I can't change the animation of the enemy to the other side
gameplay:
EnemyController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
[Header("References")]
private Animator animator;
// The target to follow.
private Transform target;
private Rigidbody2D rb;
[Header("Movement")]
public float speed;
public float range;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
target = FindObjectOfType<PlayerMovement>().transform;
}
// Update is called once per frame
void Update()
{
FollowPlayer();
}
private void FollowPlayer()
{
animator.SetBool("isRunning", true);
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
I fix it by using blend tree
Blend Tree:
Script:
private void FollowPlayer()
{
animator.SetBool("isRunning", true);
animator.SetFloat("Horizontal", target.position.x - transform.position.x);
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}

Unity accessing rigidBody component with input system

I have just started a new Unity tut using its input system to move a ball. However, the script doesn't seem to be working when I try to move the ball
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem; //Namespace for accessing InputSystem to control ball
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Onmove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement);
}
}
If you did everything else right and the problem is only in your script then changing from Onmove to OnMove should fix problem
Create in Unity a new InputControl with the name PlayerInputActions and into
Inspector select Generate C# class, Unity autogenerate for you Action Maps , Actions and Binding Property, double click on it to watch, than in a new c# script named NewInputSystem
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputSystem : MonoBehaviour
{
public Rigidbody rb;
public float moveSpeed = 7.0f;
Vector2 moveDirections = Vector2.zero;
public PlayerInputActions playerControls;
private InputAction move;
private void Awake()
{
playerControls = new PlayerInputActions();
}
private void OnEnable()
{
move = playerControls.Player.Move;
move.Enable();
}
private void OnDisable()
{
move.Disable();
}
void Start()
{
rb =gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
moveDirections = move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
//change how you want
rb.velocity = new Vector3(moveDirections.y * moveSpeed, 0, moveDirections.x * -moveSpeed);
}
}

On Line Renderer will Instantiate GameObject to make a bridge (Unity c#)

I am trying to make a bridge using Line Renderer.
With this script i write on mouse click straight line and when i want somehow use
"startPos" and "endPos" to make that streched gameobject (sprite renderer)
in 2d game.. how to write down Instatiate function? I am lost in this..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawBridge : MonoBehaviour
{
private LineRenderer lineRend;
private Vector2 mousePos;
private Vector2 startMousePos;
[SerializeField]
private SpriteRenderer spriteRender;
private float distance;
[SerializeField]
GameObject spawnBridge;
private void Start()
{
lineRend = GetComponent<LineRenderer>();
lineRend.positionCount = 2;
spriteRender = GetComponent<SpriteRenderer>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
startMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.GetMouseButton(0))
{
StartDrawn();
}
}
void StartDrawn()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 startPos = new Vector3(startMousePos.x, startMousePos.y, 0f);
lineRend.SetPosition(0, startPos);
Vector3 endPos = new Vector3(mousePos.x, mousePos.y, 0f);
lineRend.SetPosition(1, endPos);
distance = (mousePos - startMousePos).magnitude;
// Instantiate(spawnBridge, startPos, endPos, transform.rotation.);
}
}

My code to move in unity 2D game not working correctly

There is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovementScript : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value){
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, rigidbody.velocity.y);
rigidbody.velocity = moveInput;
}
}
But my character still mowing with speed of 2f and can fly up and down (i did it in first version of my code).
It looks like game not loaded newest code and don't know how to fix it.
I have selected auto refresh.

I'm trying to make a Instance at the mouse position but it doesn't work

I'm trying to make an instance at the mouse pos, but when it makes the instance, it's way off and is really far out from my mouse. How could I fix this? Here is my code:
using UnityEngine;
using System.Collections;
// note
// note
public class attack : MonoBehaviour
{
public Transform prefab;
void Update()
{
Vector3 mousePos = Input.mousePosition;
if (Input.GetMouseButtonDown(0))
Instantiate(prefab, mousePos, Quaternion.identity);
}
}
Input.mousePosition is in screen pixel space!
You most probably rather want to use Camera.ScreenToWorldSpace
public class attack : MonoBehaviour
{
public Transform prefab;
// you will need to figure this out
public float desiredDistanceInFrontOfCamera;
[SerializeFiel] private Camera _camera;
private void Awake()
{
if(!_camera) _camera = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = desiredDistanceInFrontOfCamera;
Vector3 spawnPos = _camera.ScreenToWorldPoint(mousePos);
Instantiate(prefab, spawnPos, Quaternion.identity);
}
}
}

Categories